 describe "missionService", ->
   beforeEach module 'app'
 
-  beforeEach ->
+  beforeEach inject ($rootScope, _$httpBackend_, missionService) ->
+    loadJSONFixtures('dseries.json')
+    loadJSONFixtures('mission.json')
+    loadJSONFixtures('parameters.json')
+    loadJSONFixtures('messages.geo.json')
+
     @fetchParams =
       order_by: "createdAt"
       order_dir: "desc"
       page_size: "12"
 
     @urlBase = 'https://api.3drobotics.com/api/v1'
+    @scope = $rootScope.$new()
+    @missionService = missionService
+    @httpBackend = _$httpBackend_
 
-  it 'should get missions', inject ['$httpBackend', 'missionService', ($httpBackend, missionService) ->
-    loadJSONFixtures('missions.json')
+    @httpBackend.whenGET("#{@urlBase}/auth/user").respond({"message":"You are not logged in"})
+
+  it 'should get missions', ->
     expected = getJSONFixture('missions.json')
     notExpected = [{}]
 
-    $httpBackend.expectGET("#{@urlBase}/auth/user").respond({"message":"You are not logged in"})
-    $httpBackend.expectGET("#{@urlBase}/mission").respond(expected)
+    @httpBackend.expectGET("#{@urlBase}/mission").respond(expected)
 
     positiveTestSuccess = (results) ->
       expect(results).toEqual expected

      expect(results).not.toEqual notExpected
      results

    missionService.get().then(positiveTestSuccess).then(negativeTestSuccess)

    $httpBackend.flush()
  ]

  it 'should get a mission by id', inject ['$httpBackend', 'missionService', ($httpBackend, missionService) ->
    loadJSONFixtures('mission.json')
    expected = getJSONFixture('mission.json')
    notExpected = {}

    $httpBackend.expectGET("#{@urlBase}/auth/user").respond({"message":"You are not logged in"})
    $httpBackend.expectGET("#{@urlBase}/mission/4750").respond(expected)

    positiveTestSuccess = (results) ->
      expect(results).toEqual expected

      expect(results.data).not.toEqual notExpected
      results

    @missionService.get_plotdata(4750).then(positiveTestSuccess).then(negativeTestSuccess)

    @httpBackend.flush()

  it 'should get parameters from a mission', ->
    expected = getJSONFixture('parameters.json')
    notExpected = []

    @httpBackend.expectGET("#{@urlBase}/mission/4750/parameters.json").respond(expected)

    positiveTestSuccess = (results) ->
      expect(results.data).toEqual expected
