gpt4 book ai didi

Angularjs + UnitTest BeforeEach 注入(inject)不执行

转载 作者:行者123 更新时间:2023-12-02 22:53:53 26 4
gpt4 key购买 nike

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

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) 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 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 函数没有被调用吗?

最佳答案

这里,

  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
});
}));

关于Angularjs + UnitTest BeforeEach 注入(inject)不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com