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 ...
Get Test-Driven JavaScript Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.