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 })
There are two ways to create a spy in Jasmine: spyOn() can only be used when the method already exists on the object, whereas jasmine. createSpy() will return a brand new function: //spyOn(object, methodName) where object. method() is a function spyOn(obj, 'myMethod') //jasmine.
Jasmine's createSpy() method is useful when you do not have any function to spy upon or when the call to the original function would inflict a lag in time (especially if it involves HTTP requests) or has other dependencies which may not be available in the current context.
spyOn() spyOn() is inbuilt into the Jasmine library which allows you to spy on a definite piece of code.
setting isSpy
to false
is a very bad idea, since then you spy on a spy and when Jasmine clears the spies at the end of your spec you won't get the original method. the method will be equal to the first spy.
if are already spying on a method and you want the original method to be called instead you should call andCallThrough()
which will override the first spy behavior.
for example
var spyObj = spyOn(obj,'methodName').andReturn(true); spyObj.andCallThrough();
you can clear all spies by calling this.removeAllSpies()
(this
- spec)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With