0

I have a SomeModel class defined in a third party library which is used like this:

class SystemUnderTest:
    def foo():
        ...
        with SomeModel.my_method() as model:
            x = ...
            model.bar(x)

I would like to test that calling foo() results in bar() being called on that third party model class. I'd ike to know if I can do this using @patch.object()

class MyTestCase(unitest.TestCase):

    @patch.object(SomeModel, 'my_method')
    def test_my_method_is_called(self, my_mocked_model):
        sut = SystemUnderTest()

        sut.foo()
        
        # how do I access the return value of my_mocked_model and confirm model.bar() has been called with x as an argument?

my_mocked_model is a mock that is automatically passed in to my test method by virtue of the patch.object decorator. Is there a way of asserting what calls are made to that mock object? How do I do this?

John
  • 10,837
  • 17
  • 78
  • 141
  • 1
    `model` isn't the return value of `my_method()`; it's the return value of `my_method().__enter__()`. – chepner Feb 09 '23 at 21:04

0 Answers0