How do I mock python method using python unittest.mock
which will return same value passed as argument,
I tried,
from unittest.mock import MagicMock
def dummy_function(value):
"Will return same value as value passed to the function"
return value
# To moke gettext function used in template
# Then I pass this mock method to Jinja2 template to moke gettext string
_ = MagicMock(return_value=dummy_function)
When I print the jinja template it displays test something like below,
<div class="order_details">\n
<legend class="badge"><function dummy_function at 0x10887f730></legend>\n
</div>\n
Orignal Jinja2 template has
<div class="order_details">
<legend class="badge">_('Details')</legend>
</div>
return_value
is only ever a fixed object to return, and you just told the mock that the result of a call is a function object.
You want to use the side_effect
attribute instead:
_ = MagicMock(side_effect=dummy_function)
Setting side_effect
to a function causes it to be called with the same arguments as the mock. See the documentation:
If you pass in a function it will be called with same arguments as the mock and unless the function returns the
DEFAULT
singleton the call to the mock will then return whatever the function returns.
Demo:
>>> from unittest.mock import MagicMock
>>> identity = lambda a: a
>>> MagicMock(return_value=identity)('called') # returns the function object, it won't call it
<function <lambda> at 0x10fa61620>
>>> MagicMock(side_effect=identity)('called') # will call the function
'called'