Where some_other_function is not a member of MyClass but uses an object from MyClass and calls both foo and bar methods. However when I try either mock_myclass.foo.asseert_called() or mock_myclass.bar.asseert_called() I always get the error below, even though I am positive the test is reaching the places where foo and bar are called.
AssertionError: Expected 'foo' to have been called.
The patched methods actually get called in some_other_function
The thing is that myclass has so many methods that I would like to just patch them all together in the same fixture
What you’re moving is the class (or equivalently in this case, the class constructor). In your actual module, you’ll likely be instantiating that class, then calling foo and bar on the instance. The return value of mock_myclass returns the instance, whose methods are different from what you’ve set.
You can either change your assertions to use foo etc on the return value of mock_myclass or patch all three methods of the actual class.
thanks @EpicWink !
That helped but wasn’t the entire problem.
for others that might come across this issue remember “patch where you use it, not where you define it” this means that with patch("module.file.MyClass") as myclass_mocked:
needs to be with patch("module.fileWhereMyClassIsUsed.MyClass") as myclass_mocked: