最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jasmine Expected spy myLinks to have been called error - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Oct 27, 2013 at 23:38 MichealMicheal 2,33210 gold badges52 silver badges96 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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();
    });
  });
发布评论

评论列表(0)

  1. 暂无评论