Understanding how to spy on methods in a test
Spies and stubs are closely related with the difference being that, unlike stubs, which can be used to modify data of a method or a request, spies only obtain the state of the method or request and do not have the ability to modify the method or the request. They work just like real-life spies who only track and report. Spies help us understand the execution of tests, what elements have been called, and what has been executed. In this section, we will learn about the concept of spying in Cypress, the advantages of spying on methods, and how we can utilize spying to write better Cypress tests.
Why spy?
We use spies in Cypress to record calls in a method along with the arguments of the method. By using spies, we can assert that a specific method was called a certain number of times and was called with the right arguments. We can even tell what the return values of a method were, or the execution context of the method at the time it...