angularjs - Angular & Jasmine : how to inject services with dots in their names

标签 angularjs testing

我有一个这样定义的服务:

angular.module("myApp")
  .factory("myService.foo", function () {
    // utterly delightful code
  });

我正在使用 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);
  });

});

当我尝试对名为“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);
  });

});

由于点语法的明显问题使得 Angular 无法推断服务名称。我怎样才能注入(inject)这个服务来测试它?我是否缺少一些替代语法?

最佳答案

可以使用数组表示法,例如:

var _myService;
beforeEach(inject(['myService.foo', function (myService) {
    _myService = myService;
}]));

注意(来自 documentation ):

It is important that the order of the string identifiers in the array is the same as the order of argument names in the signature of the factory function. Unless the dependencies are inferred from the function signature, it is this array with IDs and their order that the injector uses to determine which services and in which order to inject.

关于angularjs - Angular & Jasmine : how to inject services with dots in their names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19963019/

相关文章:

javascript - 在模板中加载相同的 Angular Controller 两次,但没有第二次 AJAX 调用?

php - AngularJS 使用 FormData API 上传多个文件

python - 测试具有某些代码的函数依赖于在本地测试期间不可用的服务?

ruby-on-rails - 运行单系统测试

scala - Scala 中的 Joda 编码/解码

javascript - angularjs( ionic )中的表单验证对我不起作用

javascript - AngularJS:避免使用 $timeout 等待图像加载完成

javascript - 如何链接 javascript 选择器

perl - 如何使用不断变化的系统时间测试 Perl 应用程序?

database - 单元测试数据库交互器