Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock method which returns same value passed as argument

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">&lt;function dummy_function at 0x10887f730&gt;</legend>\n            
</div>\n 

Orignal Jinja2 template has

<div class="order_details">             
<legend class="badge">_('Details')</legend>           
</div>
like image 932
Murtuza Z Avatar asked Feb 22 '18 19:02

Murtuza Z


People also ask

How do you mock the same class method?

We can mock runInGround(String location) method inside the PersonTest class as shown below. Instead of using mock(class) here we need to use Mockito. spy() to mock the same class we are testing. Then we can mock the method we want as follows.

What does Mockito mock return?

Mockito mock() method When we didn't assign anything to mocks, they will return default values. All five methods perform the same function of mocking the objects. Following are the mock() methods with different parameters: mock() method with Class: It is used to create mock objects of a concrete class or an interface.

What is difference between @mock and Mockito mock?

mock() method allows us to create a mock object of classes and interfaces. The Mockito. mock() is usually placed in the test set up method annotated with @Before in JUnit4 or @BeforeEach in JUnit 5. We called it before each test to create a new fresh mock object.

Does Mockito call real method?

Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it. To call a real method on a mocked object we use Mockito's thenCallRealMethod().


1 Answers

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'
like image 173
Martijn Pieters Avatar answered Oct 23 '22 13:10

Martijn Pieters