运行 Jasmine 2.8。
我有一个测试用例,其中失败用例是在不应该触发的条件下触发的事件处理程序。请注意,此处提供事件的代码库是专有的内部开发系统的一部分,因此这些不是 DOM 事件,并且我没有利用任何流行的 JS 框架:
it('should trigger the event handler in state 0', function (done) {
function specCb(ev) {
expect(ev).toBeDefined();
expect(ev.data).toBe('some value');
done();
}
thing.state = 0;
simulateEventTrigger(thing, specCb);
});
it('should not trigger the event handler in state 1', function (done) {
function specCb(ev) {
done.fail('this should not be called in state 1');
}
thing.state = 1;
simulateEventTrigger(thing, specCb);
});
第二个规范总是会失败,因为要么调用回调,这会显式地使规范失败,要么规范在等待调用 done()
时超时,从而导致失败。如果超时,如何让 Jasmine通过规范?
最佳答案
查看spies在 Jasmine . Spies 允许您“监视”一个函数并断言它是否已被调用以及使用哪些参数。或者,就您而言,是否还没有被调用。一个例子可能看起来像这样......
describe("A spy", function() {
var evHandlers;
beforeEach(function() {
evHandlers = {
callback: function (e) {}
}
spyOn(evHandlers, 'callback');
});
it('should not trigger the event handler in state 1', function (done) {
thing.state = 1;
simulateEventTrigger(thing, evHandlers.callback);
expect(evHandlers.callback).not.toHaveBeenCalled();
});
});
关于javascript - 传递关于异步超时的 Jasmine 规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47931641/