7

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.

4

1 回答 1

4

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

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

});
于 2014-10-21T15:26:23.383 回答