Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a service that's defined this way:

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

I'm using Karma and Jasmine for testing. In the test, I am doing something like this for most of my server tests:

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

});

When I try to do the same with a service named something like "myService.foo" it throws an error (of course):

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

});

Because of the obvious problem with the dot syntax making angular unable to infer the service name. How can I inject this service to test it? Is there some alternate syntax I'm missing?

like image 205
Sir Robert Avatar asked Dec 16 '22 04:12

Sir Robert


1 Answers

You can use the array notation, for example:

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

Note (from 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.

like image 100
tasseKATT Avatar answered Jan 26 '23 01:01

tasseKATT