How to use the spyOn Angular factory method?
In Jasmine, you can spyOn (object, 'function'). I am trying to find a vendor that is used as "vendor ()". How to do it?
The provider looks like this:
providers.provider('telecom', function() {
this.$get = function() {
return function() {
return 'something';
}
}
}
In the controller, it will 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't find anything useful. I tried spyOn (provider), but I got an error saying "undefined () method does not exist".
I even try to mock the provider but have no success. ( http://www.sitepoint.com/mocking-dependencies-angularjs-tests/ )
+3
source to share
1 answer
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();
});
});
+5
source to share