Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we clear spy programmatically in Jasmine?

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 }) 
like image 422
trivektor Avatar asked Jan 16 '12 20:01

trivektor


People also ask

How do you spy on Jasmine method?

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.

What is createSpy 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.

What is spyOn in Jasmine?

spyOn() spyOn() is inbuilt into the Jasmine library which allows you to spy on a definite piece of code.


1 Answers

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)

like image 198
Alissa Avatar answered Sep 30 '22 18:09

Alissa