@@ -11,11 +11,11 @@ System under test (SUT) refers to a system that is being tested for correct oper
1111** Mocking**
1212
1313Mocking is a replica or imitation of something. Mocking is used in unit testing. Mock objects are
14- simulated objects that mimic the behavior of real objects in controlled ways
14+ simulated objects that mimic the behavior of real objects in controlled ways.
1515A tested object may have dependencies on other complex objects. To isolate the behavior of
1616the object we want to test we replace the other objects by mocks that simulate the behavior
17- of the real objects. So in simple words , mocking is creating objects that simulate the behavior
18- of real objects.
17+ of the real objects. Mock objects never call real methods. Simply put , mocking is creating objects
18+ that simulate the behavior of real objects.
1919
2020
2121** Stub**
@@ -30,3 +30,17 @@ Stubs are using in mocking as well. We supply stub methods instead of the normal
3030return fixed results and then ensure that the code does the right thing with them.
3131This isolates testing to the code we are trying to test. For instance, we do not need
3232to spin up a database or have an online service working just to run those tests.
33+
34+
35+ ** Spy**
36+
37+ A Spy wraps an existing object. We can listen in on the conversation between the caller and
38+ the real object but retain the original object behavior. Spy delegates method calls to the
39+ original object.
40+
41+ When a class is mocked or stubbed, a test double is created and the original code that exists
42+ within the mocked or stubbed object is not executed.
43+ Spies, on the other hand, will execute the original code from which the Spy was created.
44+ A Spy also allows us to modify what the Spy returns and verify cardinality much like Mocks and Stubs.
45+
46+ Spies should be used carefully and occasionally, for example when dealing with legacy code.
0 commit comments