Answer by Tomasz Bartkowiak for Python Mock object with method called...
As in here, apart from using side_effect in unittest.mock.Mock you can also use @mock.patch.object with new_callable, which allows you to patch an attribute of an object with a mock object. Let's say a...
View ArticleAnswer by abourget for Python Mock object with method called multiple times
A little sweeter: mockobj.method.side_effect = lambda x: {123: 100, 234: 10000}[x] or for multiple arguments: mockobj.method.side_effect = lambda *x: {(123, 234): 100, (234, 345): 10000}[x] or with a...
View ArticleAnswer by Addison for Python Mock object with method called multiple times
I've ran into this when I was doing my own testing. If you don't care about capturing calls to your methodfromdepclass() but just need it to return something, then the following may suffice: def...
View ArticleAnswer by k.parnell for Python Mock object with method called multiple times
Try 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 kwarg['foo'] == 7: return "Foo is seven"...
View ArticlePython Mock object with method called multiple times
I have a class that I'm testing which has as a dependency another class (an instance of which gets passed to the CUT's init method). I want to mock out this class using the Python Mock library. What I...
View Article