I'm working on an Angular JS application that includes a module and some services. The controller in my app utilizes these services. In my Jasmine test cases, I decided to create a mock of one of the services using Jasmine's createSpy
function. Here is how I mocked the service:
beforeEach(module(function ($provide) {
shoppingData = function () {
getAllItems: jasmine.createSpy('getAllItems');
addAnItem: jasmine.createSpy('addAnItem');
removeItem: jasmine.createSpy('removeItem');
};
$provide.value('shoppingData', shoppingData);
}));
The controller triggers the getAllItems
function right after creating an object. So, I set up another beforeEach
block to instantiate the controller object. Below is the test case to verify if the getAllItems
function is called:
it("Should call getAllItems function on creation of controller", function () {
expect(shoppingData.getAllItems).toHaveBeenCalled();
});
However, when I run the spec runner page in the browser, the test fails with this error message: TypeError: 'shoppingData.getAllItems' is not a function.
I've seen similar examples where this type of test works flawlessly. Can someone help me spot what might be missing or going wrong here?
Update: I have uploaded a plunker showcasing the problematic section