how to mock a method who's been called 2 times and make it to return diferent values. To visualize in mind here's the code:
def method(self, param):
# here I have the logic if param is equal to 1 -> return [1,2,3], if param is equal to 2 -> return [a,s,d]
def method_x(self, param_x):
# here uses method returned values with 1 or 2
def main():
one = self.method(1)
two = self.method(2)
three = self.method_x(one)
four = self.method_x(two)
test:
def setUp(self):
self.c = py_file.Class()
def test_main(self):
self.c.method = MagicMock()
self.c.main()
self.c.method.assert_any_call(1)
self.c.method.assert_any_call(2)
self.assertEqual(2, self.c.method.call_count)
# but the problem comes when I need to use for other methods method(1) in three and method(2) in four
...
I have tried to use side_effect
like this:
self.c.method = MagicMock(side_effect=[[1,2,3],[a,s,d]])
# this way running test one has 1,2,3 values and two has a,s,d
but then when it comes to use the values for four
and five
I get (like c.method.side_effects= [[1,2,3],[a,s,d]] and then for four use c.method.side_effects[0] and for five with [1]
:
TypeError: 'listiterator' object is unsubscriptable
How to pass the correct values to correct varaibles in test - how to mock and set return values correctly?
Using Python2.6.6, mock 1.0.0