Writing Jasmine tests using the spyOn() method and special matchers for spies
In Jasmine, mocks are referred to as spies. Spies are used to mock a function/object method. A spy can stub any function and tracks calls and all its arguments. Jasmine provides a rich set of functions and properties to enable mocking. There are special matchers to interact with spies that are, toHaveBeenCalled
and toHaveBeenCalledWith
. In this recipe, you will learn how to mock a function using Jasmine's spyOn()
function and special matchers.
To write the Jasmine tests using spies, let's assume that you are developing an application for <ABC> company, which provides solutions for the health care industry. Currently, there is a need to design a component that can get a person's details (such as name, age, blood group, details of diseases, and so on) and process it for further usage. Now, assume that you are developing a component that verifies a person's blood or organ donation details. There are also a few...