O'Reilly logo

Test-Driven JavaScript Development by Harmeet Singh, Hetal Prajapati, Ravi Kumar Gupta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Spies

A spy is an emulation of a function or object, irrespective of which function/object is defined or not. There are times when we need stubs for the functions we need to use for performing testing. Jasmine has spies for this purpose. A spy can stub functions but can only exist within describe and it block if it is defined.

Apart from the matchers we read in this chapter, there are special matchers just for spies. One is toHaveBeenCalled(), which returns true if the spy was called. Another one is toHaveBeenCalledWith(), which returns true if the spy was called with arguments.

Let's see the following example to understand how spies work. The following is our source for an Employee function in the employee.js file created in the src folder:

var ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required