I have an AngularJS service written and I would like to unit test it.
angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
factory('myService', function ($http, fooService, barService) {
this.something = function() {
// Do something with the injected services
};
return this;
});
My app.js file has these registered:
angular
.module('myApp', ['fooServiceProvider','barServiceProvider','myServiceProvider']
)
I can test the DI is working as such:
describe("Using the DI framework", function() {
beforeEach(module('fooServiceProvider'));
beforeEach(module('barServiceProvider'));
beforeEach(module('myServiceProvder'));
var service;
beforeEach(inject(function(fooService, barService, myService) {
service=myService;
}));
it("can be instantiated", function() {
expect(service).not.toBeNull();
});
});
This proved that the service can be created by the DI framework, however next I want to unit test the service, which means mocking out the injected objects.
How do I go about doing this?
I've tried putting my mock objects in the module, e.g.
beforeEach(module(mockNavigationService));
and rewriting the service definition as:
function MyService(http, fooService, barService) {
this.somthing = function() {
// Do something with the injected services
};
});
angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
factory('myService', function ($http, fooService, barService) { return new MyService($http, fooService, barService); })
But the latter seems to stop the service being created by the DI as all.
Does anybody know how I can mock the injected services for my unit tests?
Thanks
David