Asked by: Anush Walczaasked in category: General Last Updated: 12th March, 2020
What is the difference between spy and mock?
In respect to this, what is a test spy?
A Spy is a test double that records every invocation made against it and can verify certain interactions took place after the fact. They are most often used when a subject under test triggers a side effect that can't be asserted by the value it returns.
Likewise, what is difference between mock and stub? A stub with an assertion that the method gets called. So as Sean Copenhaver described in his answer, the difference is that mocks set expectations (i.e. make assertions, about whether or how they get called). Stubs don't fail your tests, mock can.
Furthermore, what is a Mockito spy?
A Mockito mock allows us to stub a method call. That means we can stub a method to return a specific object. A Mockito spy is a partial mock. We can mock a part of the object by stubbing few methods, while real method invocations will be used for the other.
Why would you use a spy in a test?
Jasmine spies are used to track or stub functions or methods. Spies are an easy way to check if a function was called or to provide a custom return value. We can use spies to test components that depend on a service and avoid actually calling the service's methods to get a value.