我有一个以这种方式定义的服务:
angular.module("myApp")
.factory("myService.foo", function () {
// utterly delightful code
});
Run Code Online (Sandbox Code Playgroud)
我正在使用Karma和Jasmine进行测试.在测试中,我正在为我的大多数服务器测试做类似的事情:
describe('Service: someService', function () {
// load the service's module
beforeEach(module('myApp'));
// instantiate service
var _someService;
beforeEach(inject([function (someService) {
_someService = someService;
}]));
it('should do something', function () {
expect(!!_someService).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
当我尝试对名为"myService.foo"的服务执行相同操作时,它会抛出错误(当然):
describe('Service: myService.foo', function () {
// load the service's module
beforeEach(module('myApp'));
// instantiate service
var _myService;
beforeEach(inject([function (myService.foo) {
_myService = myService.foo;
}]));
it('should do something', function () {
expect(!!_myService).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
由于点语法的明显问题使得angular无法推断服务名称.如何注入此服务进行测试?我缺少一些替代语法吗?
tas*_*ATT 12
您可以使用数组表示法,例如:
var _myService;
beforeEach(inject(['myService.foo', function (myService) {
_myService = myService;
}]));
Run Code Online (Sandbox Code Playgroud)
注意(来自文档):
重要的是,数组中字符串标识符的顺序与工厂函数签名中的参数名称顺序相同.除非从函数签名推断出依赖关系,否则这个数组具有ID及其顺序,注入器用于确定哪些服务以及注入的顺序.
| 归档时间: |
|
| 查看次数: |
6026 次 |
| 最近记录: |