How to spy on anonymous function using Jasmine

↘锁芯ラ 提交于 2019-12-03 08:03:59

Daniel Smink's answer is correct, but note that the syntax has changed for Jasmine 2.0.

notify = jasmine.createSpy().and.callFake(function() {
  return false;
});

I also found it useful to just directly return a response if you only need a simple implementation

notify = jasmine.createSpy().and.returnValue(false);

You could chain your spy with andCallFake see:

http://jasmine.github.io/1.3/introduction.html#section-Spies:_andCallFake

    //create a spy and define it to change notify
    notify = jasmine.createSpy().andCallFake(function() {
      return false;
    });

    it('should be a function', function() {
        expect(typeof notify).toBe('function');             
    });

    controller = createController();
    scope.isValid('name');
    expect(notify).toHaveBeenCalled();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!