How to mock a method of an imported instance

Viewed 95

I am writing unit test for some python modules. However, I cannot work out a way to mock a method of an imported instance. Below is the python modules that I want to test.

--bar.py--

class A():
    @classmethod
    def method_1(self):
        ...    
a = A()

--foo.py--

from bar import a
class B():
    @classmethod
    def b(cls):
        if a.method_1():
            return True
        else:
            return False

--test_foo.py--

from foo import B

class TestB(TestCase):
    @patch('foo.a.method_1')
    def test_b(self, mock_method_1):
          mock_method_1.return_value = True
          test_b = B.b()
          ...

This does not work. My test case is still calling original method_1 instead of the one I mocked.

1 Answers
Related