Is it possible to have a Mock
object return a value when called as well as execute a statement when called? For example this:
> mocked_func = Mock(return_value='Returned foo',
side_effect=lambda: print('Side effect executed')
)
> print(mocked_func())
Side effect executed
None
The documentation seems a little ambiguous here but it seems to me that when side_effect
is used, then return_value
is ignored altogether and only the result of side_effect
function is returned (which in case of print()
is None
).
Since Python does not really support multi-statement lambdas (AFAIK, the best you can do is put expressions in a tuple, but then the whole tuple would be returned) - is it possible to combine the two arguments?
I want to avoid having to define a new function and using that as the side_effect
, I feel having both declared in a Mock
is cleaner.