Mocked Call Verification ======================== Verify Last Call ---------------- The general form is:: assert that().last_call == () Or if it was called with keyword arguments:: assert that().last_call == ((), {}) .. doctest:: >>> class GrailSeeker: ... def travel_to(self, place: str, when: str = "Someday"): ... pass >>> arthur = mock(GrailSeeker) >>> arthur.travel_to("Camelot") >>> assert that(arthur.travel_to).last_call == ("Camelot",) >>> arthur.travel_to("Camelot", when="Now") >>> assert that(arthur.travel_to).last_call == (("Camelot",), {"when": "Now"}) Verify Nth Call --------------- The general form is similar to checking for the last call:: assert that().nth_call() == () Where `` is a number from zero up to the number of calls made, exclusive. Verify Number of Calls ---------------------- The general form is similar to checking for the last call:: assert that().num_calls ... For example: .. doctest:: >>> assert that(arthur.travel_to).num_calls >= 1 Verify Mock Was/Not Called -------------------------- The general form is similar to checking for the last call:: assert that(). For example: .. doctest:: >>> assert that(arthur.travel_to).was_called >>> assert that(arthur.travel_to).was_not_called Traceback (most recent call last): ... AssertionError Capture Call Details -------------------- Sometimes it's useful to make more targetted or repeated assertions about a subset of what a mocked method was called with. To make this easier you can retrieve call details and assign them to a variable. To capture the details of calls that were made you can use expressions such as:: = spy().last_call = spy().nth_call() etc. .. note:: The ``spy()`` function is just a synonym for ``that()``. Example: .. doctest:: >>> class SpanishInquisition: ... def surprise(self, location: str, interrogator: str) -> str: ... raise NotImplementedError() >>> inquisitors = mock(SpanishInquisition) >>> when(inquisitors.surprise).any_call().then( ... lambda location, interrogator: ( ... f"The Spanish Inquisition surprises {location} with {interrogator}!" ... ) ... ) >>> inquisitors.surprise("the village", interrogator="Cardinal Biggles") 'The Spanish Inquisition surprises the village with Cardinal Biggles!' >>> args, kwargs = spy(inquisitors.surprise).last_call >>> args ('the village',) >>> assert args[0] == "the village" >>> kwargs {'interrogator': 'Cardinal Biggles'} >>> assert kwargs.get("interrogator") == "Cardinal Biggles"