How do we clear the spy in a jasmine test suite programmatically? Thanks.
beforeEach(function() {
spyOn($, "ajax").andCallFake(function(params){
})
})
it("should do something", function() {
//I want to override the spy on ajax here and do it a little differently
})
This worked for me in Jasmine 2.5 to allow re-setting of mock ajax.
Then you can call it multiple times without error. spyOnAjax(mock1); spyOnAjax(mock2);
So spies are reset automatically between specs.
You actually do not get the benefit of "restoration" of the original function if you use
andCallFake()
within abeforeEach()
and then attempt to forcibly change it within a spec (which is likely why it tries to prevent you from doing so).So be careful, especially if your spy is being set on a global object such as jQuery.
Demonstration:
From jasmine 2.5, you can use this global setting to update a spy within your test cases:
I'm not sure if its a good idea but you can simply set the
isSpy
flag on the function to false:But maybe its a better idea to create a new suite for this case where you need an other behavior from your spy.
I think that's what .reset() is for:
just set the spy method to null