mocksafe.LastCallStubber

class mocksafe.LastCallStubber(method_mock: MethodMock[T])

Set up results or side effects to be used when for the stub the condition matches.

Example:
>>> when(
...     mock_random.randint
... ).called_with(
...     mock_random.randint(1, 2)
... ).then_return(0.31)
Example:
>>> when(
...     mock_random.randint
... ).called_with(
...     mock_random.randint(3, 4)
... ).then_return(0.1, 0.3, 0.2)
Example:
>>> when(
...     mock_random.randint
... ).called_with(
...     mock_random.randint(5, 6)
... ).then_raise(ValueError("..."))
Example:
>>> when(
...     mock_random.randint
... ).called_with(
...     mock_random.randint(7, 8)
... ).then(
...     lambda a, b: int((b - a) / 2)
... )
Example:
>>> when(
...     mock_random.randint
... ).called_with(
...     mock_random.randint(9, 10)
... ).use_side_effects(
...     1, 2, 3, ValueError("..."), 4
... )
__init__(method_mock: MethodMock[T])

Methods

__setattr__(name, value, /)

Implement setattr(self, name, value).

then(result)

then_raise(error)

then_return(result, *consecutive_results)

use_side_effects(*side_effects)