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

I have a service that defined this method:

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

I use Karma and Jasmine for testing. In the test, I do something similar for most of my server tests:

describe('Service: someService', function () {

  // load the service 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 "myService.foo", it throws an error (of course):

describe('Service: myService.foo', function () {

  // load the service 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);
  });

});

Due to an obvious dot syntax issue that makes angular unable to display the service name. How can I use this service to verify it? Is there any alternative syntax that I am missing?

+4
source share
1 answer

You can use array notation, for example:

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

Note (from the documentation ):

, factory. , , .

+12

All Articles