Say I want to test my calls Greeter which depends a 3rd party class Foo which in turn depends on another class Bar. I need to mock Foo, but how do I set up and verify the chained call self.foo.get_bar().format(name)?
greeter.py:
class Bar:
def format(self, name):
return name.upper()
class Foo:
def __init__(self):
self.bar = Bar()
def get_bar():
return self.bar
class Greeter:
def __init__(self, foo):
self.foo = foo
def hi(self, name):
return f'Hi {self.foo.get_bar().format(name)}'
PS: this question is purely about the use of Python mock, not about the best practice of writing Python code and test, so I won't refactor the code.
After some research, I found the following solution: