I have an Angular JS application with a module and some services in it. My controller uses these services. In Jasmine test cases, I created a mock of a service using Jasmine's createSpy. Following is the mocked service:
beforeEach(module(function ($provide) {
shoppingData = function () {
getAllItems: jasmine.createSpy('getAllItems');
addAnItem: jasmine.createSpy('addAnItem');
removeItem: jasmine.createSpy('removeItem');
};
$provide.value('shoppingData', shoppingData);
}));
Controller calls the getAllItems function as soon as an object is created. I created another beforeEach block that creates an object of the controller. Following is the test block to check if getAllItems is called:
it("Should call getAllItems function on creation of controller", function () {
expect(shoppingData.getAllItems).toHaveBeenCalled();
});
When I run the spec runner page on browser, the test fails with following error: TypeError: 'shoppingData.getAllItems' is not a function
I saw several similar example where this kind of test works without any issue. Can anyone point what is missing or what is going wrong here?
Update: I created a plunker with the part that fails
Seems like a typo if this is the real code. Change the appropriated part to:
shoppingData = {
getAllItems: jasmine.createSpy('getAllItems'),
addAnItem: jasmine.createSpy('addAnItem'),
removeItem: jasmine.createSpy('removeItem')
};
Just changed the function to an object and changed the ; for ,.
UPDATE 1:
Consider only spying at the existing object:
var deferred, _shoppingData;
beforeEach(module('shopping'));
beforeEach(inject(function(shoppingData, $q) {
_shoppingData = shoppingData;
deferred = $q.defer();
spyOn(shoppingData, 'getAllItems').andReturn(deferred.promise);
}));
it('should have called shoppingData.getAllItems', function() {
expect(_shoppingData.getAllItems).toHaveBeenCalled();
});
Why don't you try:
var scope, myController;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
myController = $controller.('controllerName', {
$scope: scope,
shoppingData: jasmine.createSpyObj('shoppingData', ['getAllItems', 'addAnItem', 'removeItem'])
});
}));