我在工厂服务中使用 scope.$on 和 Jasmine 侦察绑定(bind)到事件的方法时遇到问题。最初传递的真实方法被调用,而不是 spy 。 我做了一个 plinkr:http://plnkr.co/edit/2RPwrw?p=preview
感谢您的帮助。
最佳答案
这是因为您绑定(bind)回调的方式。改变
service.$on('hello', service.method);
到
service.$on('hello', function() {
service.method();
});
当您说 spyOn(service, 'method')
时,您是在说“用 spy 替换 service.method
中引用的值。”但是,当事件被触发时,您的原始 service.$on
代码不会在 service.method 处查找值,而是在 服务已初始化。因此,将 service.method
指向的引用更改为 later 没有任何效果。
关于javascript - AngularJS + Jasmine : spy an event callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16307062/