Angularjs + UnitTest BeforeEach注入不执行

时间:2016-04-20 22:30:23

标签: angularjs coffeescript jasmine karma-jasmine

我正在尝试编写angularjs规范,但是没有执行beforeEach Inject块,导致我的测试服务未定义

  def search
    @claim = Claim.search(params[:claim_id], params[:phone_number])
  end

当我执行代码时,我收到以下错误

  

块引用       错误:[ng:areq]参数'fn'不是函数,得到了Object       http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20Object位于/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js(第1815行)       assertArg@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:1815:91       assertArgFn@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:1825:12       annotate@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:3810:16       $$annotate@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular-mocks/angular-mocks.js:2222:41       invoke@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4501:46       /Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4344:85       forEach@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:340:24       createInjector@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4344:10       workFn@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular-mocks/angular-mocks.js:2428:60       TypeError:null不是/Users/rbrooks3/sandbox/alphonso-web/src/app/components/vod-assets-triage/vod-assets-triage.spec.js中的对象(评估'$ q.defer')(第34行)       /Users/rbrooks3/sandbox/alphonso-web/src/app/components/vod-assets-triage/vod-assets-triage.spec.js:34:15

有谁知道为什么没有调用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 函数?

1 个答案:

答案 0 :(得分: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
    });
  }));