Using Spies

Learn how to use spies to mock methods.

Jasmine’s spies are helpful when the component we are testing makes calls to external functions. We can use a spy to intercept calls to external functions. In the example below, we have a function called loadData defined on line 13. The loadData function delegates the work to the index method on the bookRepository object on line 2. In a real application, the index method might call an external API or fetch the data from a database. To keep things simple, we just return an array that contains a single object representing a book.

Get hands-on with 1200+ tech skills courses.