How to spy on a custom event in jasmine?

I have a custom event defined. I want to spy on it with jasmine. But the problem I have is that it is failing when I am using spyOn to spy on that event. When I spy on some function it is working fine. Heres what I tried:

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();
    });
});

So I tried both not.toHaveBeenCalled() and toHaveBeenCalled() but it fails in both the cases. So I guess spyOn is unable to spy on the custom event.

*Note: * I looked at other SO answers with a similar question, but it was something to do with a click event. But in my case it is a custom event that will get fired based on some conditions automatically.


Try something like this. Worked for me

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

});
链接地址: http://www.djcxy.com/p/73126.html

上一篇: 从Lumia 1020访问气压计数据

下一篇: 如何窥视茉莉花中的自定义事件?