mercredi 29 avril 2015

How to access object instance from mocked instance method

In the code below where it says foo_obj = ????, how can I get a reference to the Foo object instance, or what could be a better approach?

class Foo(object):

    def __init__(self):
        self.hello = "Hey!"

    def bar(self):
        return self.hello + " How's it going?"

def side_effect_foo_bar(*args, **kwargs):
    foo_obj = ????
    return foo_obj.hello + " What's up?"

class TestFoo(unittest.TestCase):

    @patch.object(Foo, 'bar')
    def test_bar(self, mocked_bar):
        mocked_bar.side_effect = side_effect_foo_bar
        foo = Foo()
        self.assertTrue(foo.bar() == "Hey! What's up?")

Aucun commentaire:

Enregistrer un commentaire