How to mock a method so it returns the argument you pass it
How to mock a method so it returns the argument you pass it
March 23, 2016
While I was refactoring and adding tests for a pretty hairy method, I found that I wanted to mock out some methods on the object so that they just returned whatever value was passed into them. It turns out that's pretty easy to do, although it took me a little while to figure it out. To save you (and future me) the time, here's how.
Given an instance [request]{.title-ref} with a method [complex_method]{.title-ref} which takes a single argument, replace [complex_method]{.title-ref} with a mock which returns the argument unchanged:
from mock import Mock
request.complex_method = Mock(side_effect=lambda arg: arg)Subsequent calls to [complex_method]{.title-ref} will return unchanged whatever you pass it:
>>> request.complex_method(True)
True
>>> request.complex_method(False)
False
>>> request.complex_method('amazing!')
'amazing!'