Reputation: 2149
I have a jasmine spy installed on a method, which is called like
if(typeof method === 'function'){
var context = {a:'b'};
method.call(context);
}
so that when the method is called, the this
keyword inside it will be context. How do I test the context of the spy with Jasmine?
Upvotes: 1
Views: 634
Reputation: 904
I like the Koen's answer but I decided to do it more elegant way using custom matchers. Here is the lib https://www.npmjs.com/package/jasmine-spy-matchers
Using this lib you can do the following:
expect(method).toHaveBeenCalledWithContext(context);
Upvotes: 0
Reputation: 2472
The calling context is in the object
property of each calls object.
So given
var method = jasmine.createSpy();
var context = {a:'b'};
method.call(context);
You can test the calling context of your spied method like this:
mostRecentCall = method.calls.mostRecent();
expect(mostRecentCall.object).toEqual(context);
Check here for more examples in the jasmine docs.
Upvotes: 2