2016-04-20 86 views
3

我試圖寫一個angularjs規範,但beforeEach進樣塊沒有被執行,留給我的測試服務未定義Angularjs +單元測試BeforeEach進樣不執行

describe 'VodAssetsTriageController', -> 
    $charterModal = null 
    controller = null 
    FieldService = null 
    $q = null 
    $rootScope = null 
    $scope = null 
    VodAssetsService = null 
    PidService = 
    getPids: _.noop 
    AdiSearchService = 
    getHeadEnds: _.noop 

    beforeEach module('alphonso', ($provide) -> 
    $provide.factory 'AdiSearchService', 
     $get: -> AdiSearchService 
    $provide.factory 'PidService', 
     $get: -> PidService 
) 


    beforeEach inject ($controller, _VodAssetsService_, _VodAssetsFieldService_,   _$charterModal_, _$q_, _$rootScope_, _AdiSearchService_, _PidService_) -> 

    controller = $controller 
    $charterModal = _$charterModal_ 
    VodAssetsService = _VodAssetsService_ 
    FieldService = _VodAssetsFieldService_ 
    $q = _$q_ 
    $rootScope = _$rootScope_ 
    $scope = $rootScope.$new() 
    AdiSearchService = _AdiSearchService_ 
    PidService = _PidService_ 

    result = 
     content: [] 
    spyOn PidService, 'getPids' 
     .and.returnValue $q.when(result) 
    spyOn AdiSearchService, 'getHeadEnds' 
     .and.returnValue $q.when(result) 

    controller = controller 'VodAssetsTriageCtrl', 
     VodAssetsService: VodAssetsService, 
     VodAssetsFieldService: FieldService, 
     $scope: $scope, 
     $charterModal: $charterModal 

    installPromiseMatchers() 

    describe 'controller.paginate.onChange()', -> 
    result = null 
    searchDeferred = null 

    beforeEach -> 
     page = 
     size: 1000 
     totalElements: 179000 
     response = 
     content: [] 
     number: 0 
     page: page 
     result = 
     response: response 
     searchDeferred = $q.defer() 
     spyOn VodAssetsService, 'getAll' 
     .and.returnValue searchDeferred.promise 
     $scope.searchVm = 
     doSearch:() -> controller.search() 

    it 'should invoke controller.paginate.onChange() on search() invocation', -> 
     controller.search controller 
     expect controller.paginate.loading 
     .toBeTruthy() 

     spyOn controller.paginate, 'onChange' 
     promise = searchDeferred.resolve(result) 
     $rootScope.$apply() 

     expect promise 
     .toBeResolvedWith response 
     expect vm.paginate.totalElements 
     .toBe 179 
     expect controller.page 
     .toBe result.response.page.number 
     expect controller.size 
     .toBe result.response.page.size 
     expect controller.paginate.loading 
     .toBeFalsy() 
     expect controller.paginate.onChange 
     .toHaveBeenCalled() 

    it 'should page to vm.paginate.page = 1', -> 
     controller.paginate.page = 1 
     controller.paginate.cache[0] = _.range 0, 1000 
     spyOn $scope.searchVm, 'doSearch' 
     controller.paginate.onChange() 

     expect vm.page 
     .toBe 1 
     expect $scope.searchVm.doSearch 
     .not.toHaveBeenCalled() 
     expect controller.vodAssets.length 
     .toBe vm.paginate.size 

當我執行我收到的代碼以下錯誤

Blockquote Error: [ng:areq] Argument 'fn' is not a function, got Object http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20Object in /Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js (line 1815) [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:1815:91 [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:1825:12 [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:3810:16 [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular-mocks/angular-mocks.js:2222:41 [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4501:46 /Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4344:85 [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:340:24 [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4344:10 [email protected]/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular-mocks/angular-mocks.js:2428:60 TypeError: null is not an object (evaluating '$q.defer') in /Users/rbrooks3/sandbox/alphonso-web/src/app/components/vod-assets-triage/vod-assets-triage.spec.js (line 34) /Users/rbrooks3/sandbox/alphonso-web/src/app/components/vod-assets-triage/vod-assets-triage.spec.js:34:15

有沒有人知道爲什麼beforeEach inject函數沒有被調用?

回答

0

這裏,

beforeEach(module('alphonso', function($provide) { 
    $provide.factory('AdiSearchService', { 
     $get: function() { 
     return AdiSearchService; 
     } 
    }); 
    return $provide.factory('PidService', { 
     $get: function() { 
     return PidService; 
     } 
    }); 
    })); 

模塊配置的功能不應該返回任何東西,並返回一個對象。

而且,它顯然應該是$provide.provider,而不是$provide.factory,服務嘲諷短語法

beforeEach(module('alphonso', function($provide) { 
    $provide.factory('AdiSearchService', { 
     AdiSearchService: AdiSearchService, 
     PidService: PidService 
    }); 
    }));