如何监视茉莉花中的自定义事件?

Ind*_*pta 7 javascript testing 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();
    });
});
Run Code Online (Sandbox Code Playgroud)

所以我尝试了两种情况not.toHaveBeenCalled(),toHaveBeenCalled()但两种情况都失败了.所以我猜spyOn是无法窥探自定义事件.

*注意:*我用类似的问题查看了其他SO答案,但这与点击事件有关.但在我的情况下,这是一个自动触发基于某些条件的自定义事件.

Kat*_*nko 4

尝试这样的事情。为我工作

describe("Test:", function(){
it("Expects event will be spied: ", function() {
    var eventSpy = jasmine.createSpy();
    sampleElement.addEventListener('sample event', eventSpy);
    expect(eventSpy).toHaveBeenCalled();

});
Run Code Online (Sandbox Code Playgroud)