2015-11-20 56 views
0

我已經開始使用jasmine在angularjs中測試我的控制器,但在閱讀了一些教程後,我有點卡住了。如何測試我的服務使用jasmine和angularjs返回數據

我有一個名爲jasmineController

(function() { 
    "use strict"; 

    var myAppModule = angular.module('myApp'); 

    myAppModule.controller('jasmineController', ['$scope', 'genericService', 
     function ($scope, genericService) { 
      $scope.name = 'Superhero'; 
      $scope.counter = 0; 
      $scope.$watch('name', function (newValue, oldValue) { 
       $scope.counter = $scope.counter + 1; 
      }); 

      $scope.testPromise = function() { 
       return genericService.getAll("dashboard", "currentnews", null, null, null); 
      } 

      $scope.getNewsItems = function() { 
       genericService.getAll("dashboard", "currentnews", null, null, null).then(function (data) { 

        $scope.name = 'Superhero'; 
        $scope.newsItems = data; 

       }); 
      } 

     } 
    ]); 
})(); 

這個簡單angularjs控制器在我的茉莉花測試,我想叫getNewsItems並檢查它可以調用genericService.getAll和$ scope.newsItems分配一些數據。我知道我會嘲笑這項服務,但我實際上不會打電話給它。

這裏是我的規格

describe("test", function() { 
    // Declare some variables required for my test 
    var controller, scope, genericService; 

    // load in module 
    beforeEach(module("myApp")); 


    beforeEach(inject(function ($rootScope, $controller, _genericService_) { 
     genericService = _genericService_; 
     // assign new scope to variable 
     scope = $rootScope.$new(); 
     controller = $controller('jasmineController', { 
      '$scope': scope 
     }); 
    })); 
    it('sets the name', function() { 
     expect(scope.name).toBe('Superhero'); 
    }); 

    it('should assign data to scope', function() { 
     //var fakeHttpPromise = {success: function() { }}; 
     scope.getNewsItems(); 
     spyOn(genericService, 'getAll'); 
     expect(genericService.getAll).toHaveBeenCalledWith('dashboard', 'currentnews'); 
    }); 

}); 

我有一個spyon爲genericService.getall(),但除此之外,我堅持檢查我的範圍變量賦值了一下。

我也得到這個堆棧跟蹤:

Error: Expected spy getAll to have been called with [ 'dashboard', 'currentnews' ] but it was never called. 
    at stack (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:1441:11) 
    at buildExpectationResult (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:1408:5) 
    at expectationResultFactory (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:533:11) 
    at Spec.prototype.addExpectationResult (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:293:5) 
    at addExpectationResult (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:477:9) 
    at Anonymous function (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:1365:7) 
    at Anonymous function (file:///C:/Projects/2013/AMT2015/AMT2015.WebAPP/Scripts/tests/controllers/dashboardControllerSpec.js:49:9) 
    at attemptSync (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:1759:9) 
    at QueueRunner.prototype.run (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:1747:9) 
    at QueueRunner.prototype.execute (file:///C:/Users/nickgowdy/Local%20Settings/Application%20Data/Microsoft/VisualStudio/12.0/Extensions/4sg2jkkc.gb4/TestFiles/jasmine/v2/jasmine.js:1733:5) 
+0

您需要返回genericService.getAll用於測試目的,那麼只有您可以測試 – ngLover

+0

@ngLover我需要調用:spyOn(genericService,'getAll')。和Return(falseHttpPromise)?我之前在我的代碼中使用過這個,但它沒有通過這個測試。 –

+0

也嘗試在null中傳入nullHaveBeenCalledWith – ngLover

回答

1

我落得這樣做:

describe("test", function() { 
    // Declare some variables required for my test 
    var controller, scope, genericService; 

    // load in module 
    beforeEach(module("myApp")); 


    beforeEach(inject(function ($rootScope, $controller, _$q_, _genericService_) { 
     genericService = _genericService_; 
     var deferred = _$q_.defer(); 
     deferred.resolve('resolveData'); 
     spyOn(genericService, 'getAll').and.returnValue(deferred.promise); 

     scope = $rootScope.$new(); 
     controller = $controller('jasmineController', { 
      '$scope': scope 
     }); 
    })); 
    it('sets the name', function() { 
     expect(scope.name).toBe('Superhero'); 
    }); 

    it('should assign data to scope', function() { 
     //spyOn(genericService, 'getAll').and.callFake(function() { 

     //}); 
     scope.getNewsItems(); 
     scope.$apply(); 
     expect(scope.newsItems).toBe('resolveData'); 
     //expect(genericService.getAll).toHaveBeenCalledWith('dashboard', 'currentnews', null, null, null); 

    }); 

}); 

因爲我的測試不僅僅是調用一個服務,但處理一個承諾以及我不得不注入$ Q更多。然後用間諜方式說我要調用服務和方法,返回值是延期承諾。

最後,我可以看的範圍變量,看是否有任何與此行分配:

expect(scope.newsItems).toBe('resolveData'); 

感謝大家,幫助。

1

您需要調用測試功能之前,首先把間諜。而你實際上是將更多參數傳遞給服務功能。所以你需要用確切的參數列表進行測試。

it('should assign data to scope', function() { 
     //var fakeHttpPromise = {success: function() { }}; 
     spyOn(genericService, 'getAll'); 
     scope.getNewsItems(); 
     expect(genericService.getAll).toHaveBeenCalledWith('dashboard', 'currentnews',null,null,null); 
    }); 
+0

你是對的,如果我調用$ scope.testPromise我的測試通過,但如果我調用$ scope.getNewsItems它不。我的控制器中的大部分角碼都遵循$ scope.getNewsItems,其中函數不會返回任何內容,而是在代碼中將解析的promise分配給另一個範圍變量。 –

+0

你知道我會如何測試getNewsItems嗎?據我所見,它們都使用genericservice,但getNewsItems在.then之後有一個惱人的函數 –

相關問題