Just wanted to share how to get this to work with side_effect
. I put together a simplified version to showcase how to use side_effect
for your use case.
side_effect
behaves differently than return_value, where when you provide a side_effect
with a list with entries, what you are stating actually is that, each time your mocked method is called it will return each item in the list as its return value. This is actually why you are getting ValueError: too many values to unpack (expected 3), because by doing this:
[1, 2, 3]
You are saying, for each call to my mocked method, return 1, then the next time I call the method, return 2, then return 3.
With that in mind, if you set up your side_effect
, like this:
[('stuff1', 'stuff2', 'stuff3')]
What you are now saying, is that, for when you call side_effect, the first item in the list is what will be returned. Which, in effect is:
('stuff1', 'stuff2', 'stuff3')
Alternatively, you can do this:
my_test_foo.side_effect = lambda x, y: (1, 2, 3)
Which mimics out the method you are testing by taking two args and returning the three values it should return.
So, with this in mind, your test can be structured as:
import unittest
from unittest.mock import Mock
from mock import patch
from stuff import FunctionIWantToTest
class MyTest(unittest.TestCase):
@patch('stuff.myExampleFunction', return_value=Mock())
def test_mytest(self, m_example_function):
m_example_function.side_effect = [("stuff1", 100, 200)]
# m_example_function.side_effect = lambda x, y: ("stuff1", 100, 200)
a, b, c = FunctionIWantToTest()
if __name__ == '__main__':
unittest.main()