Click here to Skip to main content
15,887,453 members
Articles / All Topics
Technical Blog

Jasmine 2 Spy Cheat Sheet

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 Jan 2016CPOL1 min read 7.9K   1  
Jasmine 2 Spy Cheat Sheet

If you’re tired of Googling for the Jasmine docs every time you need to use a spy, look no further!

Jasmine is my testing framework of choice when I’m writing Angular. And I was just getting used to all those spy methods with the help of Toby Ho’s cheat sheet, and then Jasmine 2 came along and changed all the method names.

So here’s an updated cheat sheet for spying with Jasmine 2.

Create a spy

Spy on an existing method
JavaScript
spyOn(obj, 'method');   // same as Jasmine 1
Create a new function to use as a spy
JavaScript
jasmine.createSpy('optional name');    // same as Jasmine 1
Create a new object with spy functions as properties
JavaScript
jasmine.createSpyObj('name', ['fn1', 'fn2', ...]);    // same as Jasmine 1

Modify Behavior When the spy is Called

Default spy behavior is to record the call with its context/arguments and stop there. Spies will not call through to the spied function by default. These calls all modify that behavior.

Call through to the original
JavaScript
obj.method.and.callThrough();
Return the specified value
JavaScript
obj.method.and.returnValue(val);
Call the given function instead of the real one
JavaScript
obj.method.and.callFake(function() {...});
Throw an error
JavaScript
obj.method.and.throwError(err);
Reset to default stubbing behavior (resets the operations above)
JavaScript
obj.method.and.stub();

Verifying and Counting Calls on a spy

Every call to a spy is exposed on the calls property

Returns true if any calls have been made
JavaScript
obj.method.calls.any();
Returns the number of times the spy got called
JavaScript
obj.method.calls.count();
Reset the call tracker
JavaScript
obj.method.calls.reset();

After calling reset(), any() will be false, count() will be 0, etc.

Returns the first call’s context and arguments
JavaScript
obj.method.calls.first();

It will return an object like this:

JavaScript
{
  object: {...},  // 'this' object
  args: []        // array of arguments
}
Returns the most recent call’s context and arguments
JavaScript
obj.method.calls.mostRecent();

It will return an object like this:

JavaScript
{
  object: {...},  // 'this' object
  args: []        // array of arguments
}
Returns array of context and arguments passed to each call
JavaScript
obj.method.calls.all();

The returned array looks like this:

JavaScript
[
  {
    object: {...},  // 'this' object
    args: [...]     // array of arguments
  },  
  ...               // one object for each call
]

That’s it! Go test some stuff.

Jasmine 2 Spy Cheat Sheet was originally published by Dave Ceddia at Angularity on September 30, 2015.

This article was originally posted at https://daveceddia.com/feed.xml

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Dave is a Software Engineer in the Boston area and writes about AngularJS and other JavaScript things over at daveceddia.com

Comments and Discussions

 
-- There are no messages in this forum --