我定义了一个自定义事件。我想用 Jasmine 来窥探它。但我遇到的问题是,当我使用 spyOn
监视该事件时,它失败了。当我监视某些功能时,它工作正常。这是我尝试过的:
describe("Test:", function(){
it("Expects event will be spied: ", function() {
var eventSpy = spyOn(window, 'myEvent').andCallThrough();
expect(eventSpy).toHaveBeenCalled();
//Also tried this:
//expect(eventSpy).not.toHaveBeenCalled();
});
});
所以我尝试了 not.toHaveBeenCalled()
和 toHaveBeenCalled()
但这两种情况都失败了。所以我猜 spyOn
无法监视自定义事件。
*注意:* 我查看了类似问题的其他 SO 答案,但它与点击事件有关。但在我的例子中,这是一个自定义事件,会根据某些条件自动触发。
最佳答案
尝试这样的事情。为我工作
describe("Test:", function(){
it("Expects event will be spied: ", function() {
var eventSpy = jasmine.createSpy();
sampleElement.addEventListener('sample event', eventSpy);
expect(eventSpy).toHaveBeenCalled();
});
关于javascript - 如何监视 Jasmine 中的自定义事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917068/