Madh
Madh

Reputation: 33

Call the side_effect function each time the mock method is called instead of at the beginning?

I need to mock an object with a bunch of methods. For all except one Mock() is perfect for me. But for one of the methods, I actually need to call a function whenever the method is called during the run.

Basically, I am doing some basic threading, so for my code to work, I can't give it the return values manually before the run. The function has to be called during the run.

m = Mock()
m.meth.side_effect = foo()

When I do this, as expected, it calls foo() at the start, and uses that value permanently

So, I would like it such that whenever during the run - m.meth is called - foo() is called (and value returned)

Upvotes: 1

Views: 1235

Answers (1)

Aligzandr
Aligzandr

Reputation: 36

I'm not sure if you found the answer yet but you should just assign and not call the method as well.

m = Mock()
m.meth.side_effect = foo

Upvotes: 2

Related Questions