Question

I am having a hard time understanding jasmine spyOn function. I wrote a simple function and test if my method was called:

function myView() {
  myLinks();
}

Here are my tests:

describe('#myView', function() {
    it('updates link', function() {
      var spyEvent = spyOn(window, 'myLinks');
      expect(spyEvent).toHaveBeenCalled();
    });
  });

This returns the following failure:

Expected spy myLinks to have been called

What am i doing wrong here?

Was it helpful?

Solution

You need to call the myView() function so the myLinks() have been called.

function myLinks(){
    //some tasks
}    

function myView() {
  myLinks();
}

This two function above are declared in window object, then you create a spy object pointing to the window.

describe('#myView', function() {
    myView();//Call the method so the myLinks was called too
    it('updates link', function() {
      var spyEvent = spyOn(window, 'myLinks');
      expect(spyEvent).toHaveBeenCalled();
    });
  });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top