我想确保在触发自定义 jQuery 事件时将对象的方法作为事件处理程序调用;但是单元测试似乎返回了一个假阴性,因为我的实现运行良好。
(这是使用Twitter Flight和Flight Jasmine 扩展的测试套件的一部分,但这只是一个普通的 Jasmine 间谍。)
describe('listening for uiNeedsPlan event', function() {
var spy;
beforeEach(function() {
spy = spyOn(this.component, 'getPlan');
$(document).trigger('uiNeedsPlan');
});
it('gets the current plan', function() {
expect(spy).toHaveBeenCalled();
});
});
这会导致规范失败:
Expected spy getPlan to have been called.
这是我的 Flight 组件的代码实现的片段(成功运行):
this.after('initialize', function () {
this.on(document, 'uiNeedsPlan', this.getPlan);
});