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