我很难理解 Jasmine spyOn函数。
我编写了一个简单的函数并测试是否调用了我的方法:

function myView() {
  myLinks();
}

这是我的测试:
describe('#myView', function() {
    it('updates link', function() {
      var spyEvent = spyOn(window, 'myLinks');
      expect(spyEvent).toHaveBeenCalled();
    });
  });

这将返回以下故障:
Expected spy myLinks to have been called

我在这里做错了什么?

最佳答案

您需要调用myView()函数,以便已调用myLinks()

function myLinks(){
    //some tasks
}

function myView() {
  myLinks();
}

上面的这两个函数在窗口对象中声明,然后创建一个指向该窗口的 spy 对象。
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();
    });
  });

07-24 17:32