mocksafe.MatchCallStubber
- class mocksafe.MatchCallStubber(method_mock: MethodMock[T], matcher: CallMatcher)
Set up results or side effects to be used when for the stub the condition matches.
- Example:
>>> when(mock_random.random).any_call().then_return(0.31)
- Example:
>>> when(mock_random.random).any_call().then_return(0.1, 0.3, 0.2)
- Example:
>>> when(mock_random.random).any_call().then_raise(ValueError("..."))
- Example:
>>> when(mock_random.randint).any_call().then(lambda a, b: int((b - a) / 2))
- Example:
>>> when( ... mock_random.randint ... ).any_call().use_side_effects( ... 1, 2, 3, ValueError("..."), 4 ... )
- __init__(method_mock: MethodMock[T], matcher: CallMatcher)
Methods
__setattr__(name, value, /)Implement setattr(self, name, value).
then(result)Use a custom lambda to produce stubbed results.
then_raise(error)Raise an exception.
then_return(result, *consecutive_results)Set one or more results to be returned by the method stub.
use_side_effects(*side_effects)Specify an ordered sequence of results and/or exceptions to be returned/raised.