Reputation: 722
In Jasmine, you can spyOn(object, 'function'). I am trying to spyOn a provider, which is used as "provider()". How to spyOn it?
The provider looks like this:
providers.provider('telecom', function() {
this.$get = function() {
return function() {
return 'something';
}
}
}
In controller, it would be used like this:
controllers.controller('ctrl', function(telecom) {
var isp = telecom();
});
For object.method(), we can spyOn(object, 'method'). What about provider()?
I've googled and can not find anything helpful. I tried spyOn(provider), but I got error saying "undefined() method does not exist".
I even try to mock the provider, but didn't success. (http://www.sitepoint.com/mocking-dependencies-angularjs-tests/)
Upvotes: 3
Views: 2368
Reputation: 38490
You can use createSpy:
describe('Describe', function() {
var $scope, createController;
var telecomSpy = jasmine.createSpy('telecomSpy');
beforeEach(module('myApp'));
beforeEach(inject(function($rootScope, $controller) {
$scope = $rootScope.$new();
createController = function() {
$controller('MyController', {
$scope: $scope,
telecom: telecomSpy
});
};
}));
it('It', function() {
expect(telecomSpy).not.toHaveBeenCalled();
createController();
expect(telecomSpy).toHaveBeenCalled();
});
});
Demo: http://plnkr.co/edit/bdGZtOKV9mewQt9hteDo?p=preview
Upvotes: 5