I have a certain piece of code that looks like this:
# file1.py
from module import Object
def method():
o = Object("param1")
o.do_something("param2")
I have unittests that look like this:
@patch("file1.Object")
class TestFile(unittest.TestCase):
def test_call(self, obj):
...
I can do obj.assert_called_with() in the unittest to verify that the constructor was called with certain parameters. Is it possible to verify that obj.do_something was called with certain parameters? My instinct is no, as the Mock is fully encapsulated within Object, but I was hoping there might be some other way.
You can do this, because the arguments are passed to the mock object.
This should work:
@patch("file1.Object")
class TestFile:
def test_call(self, obj):
method()
obj.assert_called_once_with("param1")
obj.return_value.do_something.assert_called_once_with("param2")
obj.return_value is the Object instance (which is a MagickMock object with the Object spec), and do_something is another mock in that object that is called with the given parameter.
As long as you are just passing arguments to mock objects, the mock will record this and you can check it. What you don't have is any side effects from the real function calls - so if the original do_something would call another function, this cannot be checked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With