1

I have a test like:

import somemodule
import somemodule2

class SomeTestCase(unittest.TestCase):

  def setUp(self):
    super().setUp()
    self.ft_mock = mock.MagicMock(spec=somemodule.SomeClass,
                                  name='mock_it')

  def tearDown(self) -> None:
    self.ft_mock.reset_mock(return_value=True, side_effect=True)

  @mock.patch('somemodule2.someFunk2',
                     name = 'mock_2',
                     spec=True,
                     side_effect='some val')
  def testSomething(self, mock_rr):
    m = self.ft_mock
    m.addMemberSomething()
    self.assertEqual(len(m.addMember.call_args_list), 1)
    m.addSomethingElse.return_value = 'RETURN IT'
    m.addSomethingElse()
    m.addSomethingElse.assert_called_once()
    res = somemodule.FooClass.foo()
    mock_rr.assert_called()

Here somemodule.FooClass.foo() internally calls someFunk2 from somemodule2 which has been mocked as mock_rr

In the test debug it does call it as i print out a line from someFunk2 but testing it when mock_rr.assert_called() is called, it throws:

AssertionError: Expected 'mock_2' to have been called.

I've tried several ways using patch and patch.object

user2290820
  • 2,709
  • 5
  • 34
  • 62

1 Answers1

1

The issue was that somemodule also imported someFunk2 from somemodule2. So when patching/mocking, the patched object was from somemodule2 while the object that Needed to be patched was somemodule.someFunk2

user2290820
  • 2,709
  • 5
  • 34
  • 62