How can I spy on a custom event in Jasmine?

1,099 views
Skip to first unread message

Indradhanush Gupta

unread,
Jul 29, 2013, 5:59:30 AM7/29/13
to jasmi...@googlegroups.com
I am trying to spy on a custom event. 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. Is my approach wrong?

Eddy Josafat Hernández Vega

unread,
Jul 29, 2013, 2:18:28 PM7/29/13
to jasmi...@googlegroups.com
Have you tried to spy on the function triggered when the event is fired?

David Linse

unread,
Jul 30, 2013, 12:15:04 PM7/30/13
to jasmi...@googlegroups.com

don't you have to trigger the event..?

<code>


describe("Test:", function(){ it("Expects event will be spied: ", function() { var eventSpy = spyOn(window, 'myEvent').andCallThrough();

window.trigger('myEvent');

expect(eventSpy).toHaveBeenCalled();
</code>

//david

> --
> You received this message because you are subscribed to the Google Groups "Jasmine" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
> To post to this group, send email to jasmi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/jasmine-js.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

Reply all
Reply to author
Forward
0 new messages