javascript – 将模拟服务注入角度测试的未知问题
作者:互联网
这是我的模块声明:
(angular.module 'services.date', [])
.service '$date', () ->
# stuff
this
这是我的注射码:
describe 'Service: dateService', () ->
dateService = null
beforeEach module 'services.date'
beforeEach inject (_$date_) ->
dateService = _$date_
describe 'Test', () ->
it 'should test stuff', () ->
(expect true).toBe true
如果我运行此代码,我会收到此错误:
Firefox 37.0.0 (Ubuntu 0.0.0) Service: dateService Test should test stuff FAILED
minErr/<@/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:68:12
loadModules/<@/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:4411:15
forEach@/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:336:11
loadModules@/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:4372:5
createInjector@/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:4297:11
workFn@/home/pv/Sites/web/ngbp-coffee/vendor/angular-mocks/angular-mocks.js:2172:44
现在这里是有趣的部分:如果我注释掉注入代码,测试通过:
describe 'Service: dateService', () ->
# dateService = null
# beforeEach module 'services.date'
# beforeEach inject (_$date_) ->
# dateService = _$date_
describe 'Test', () ->
it 'should test stuff', () ->
(expect true).toBe true
//跑完之后
Firefox 37.0.0 (Ubuntu 0.0.0): Executed 1 of 1 SUCCESS (0.003 secs / 0.002 secs)
Done, without errors.
关于我如何注入错误的服务是什么意思?请帮忙.
UPDATE
看起来$injector服务无法识别我的其他服务.如果我做
$injector.has '$date'
我弄错了
WTF我做错了?????
解决方法:
仅当您不想使用注入的引用(_ $date _)隐藏变量(dateService)时,“Underscore Wrapping”才有用.这意味着使用_ $date _仅在您想要调用变量“$date”(而不是dateService)时才有用.
但是,它无法解决您的问题. $injector应该有’$date’.我想如果没有更多的代码,我们无法帮助你.您的工厂声明或给Karma的配置文件可能有问题,因为很明显,“$date”尚未加载.
相同的代码对我有用(但是空工厂).
标签:javascript,angularjs,karma-runner,karma-jasmine 来源: https://codeday.me/bug/20190711/1433451.html