Pattern – spies
Mocks allow us to replace an object or class with a dummy mock object. We've seen how we can then make the mock return predefined values, so that the class under test doesn't even know that it has made a call to a mock object. However, sometimes we might want to just record that the call was made to an object, but allow the execution flow to continue to the real object and return. Such an object is known as a spy. A spy retains the functionality of recording calls and being able to assert on the calls afterwards, but it does not replace a real object like a regular mock does.
The wraps
parameter when creating a mock.Mock
object allows us to create spy behavior in our code. It takes an object as a value, and all calls to the mock are forwarded to the object we pass, and the return value is sent back to the caller. The following is an example:
def test_action_doesnt_fire_if_rule_doesnt_match(self): goog = Stock("GOOG") exchange = {"GOOG": goog} rule = PriceRule("GOOG...