We have been using Mock for python for a while.
Now, we have a situation in which we want to mock a function
def foo(self, my_param): #do something here, assign something to my_result return my_result
Normally, the way to mock this would be (assuming foo being part of an object)
self.foo = MagicMock(return_value="mocked!")
Even, if i call foo() a couple of times i can use
self.foo = MagicMock(side_effect=["mocked once", "mocked twice!"])
Now, I am facing a situation in which I want to return a fixed value when the input parameter has a particular value. So if let's say "my_param" is equal to "something" then I want to return "my_cool_mock"
This seems to be available on mockito for python
when(dummy).foo("something").thenReturn("my_cool_mock")
I have been searching on how to achieve the same with Mock with no success?
Any ideas?
If
side_effect_func
is a function then whatever that function returns is what calls to the mock return. Theside_effect_func
function is called with the same arguments as the mock. This allows you to vary the return value of the call dynamically, based on the input:>>> def side_effect_func(value): ... return value + 1 ... >>> m = MagicMock(side_effect=side_effect_func) >>> m(1) 2 >>> m(2) 3 >>> m.mock_calls [call(1), call(2)]
http://www.voidspace.org.uk/python/mock/mock.html#calling
As indicated at Python Mock object with method called multiple times
A solution is to write my own side_effect
def my_side_effect(*args, **kwargs): if args[0] == 42: return "Called with 42" elif args[0] == 43: return "Called with 43" elif kwargs['foo'] == 7: return "Foo is seven" mockobj.mockmethod.side_effect = my_side_effect
That does the trick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With