【发布时间】:2013-06-23 19:18:42
【问题描述】:
我想确保在触发自定义 jQuery 事件时,对象的方法作为事件处理程序被调用;但单元测试似乎返回了一个假阴性,因为我的实现运行良好。
(这是使用 Twitter Flight 和 Flight Jasmine extensions 的测试套件的一部分,但这只是一个普通的 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 组件的代码实现中的一个 sn-p(成功运行):
this.after('initialize', function () {
this.on(document, 'uiNeedsPlan', this.getPlan);
});
【问题讨论】:
-
您是否检查过您的
this.after...回调是否已使用debugger或console.log调用。顺便提一句。监视您要测试的对象的功能不是一个好主意。
标签: javascript unit-testing jasmine twitter-flight