Mocking only a single method on an object

前端 未结 1 814
耶瑟儿~
耶瑟儿~ 2020-12-20 11:13

I\'m familiar with other mocking libraries in other languages such as Mockito in Java, but Python\'s mock library confuses the life out of me.

I have th

相关标签:
1条回答
  • 2020-12-20 11:38

    I think what you are looking for is mock.patch.object

    with mock.patch.object(MyClassUnderTest, "submethod") as submethod_mocked:
        submethod_mocked.return_value = 13
        MyClassUnderTest().main_method()
        submethod_mocked.assert_called_once_with(user_id, 100, self.context,
                                                 self.account_type)
    

    Here is small description

     patch.object(target, attribute, new=DEFAULT, 
                  spec=None, create=False, spec_set=None, 
                  autospec=None, new_callable=None, **kwargs)
    

    patch the named member (attribute) on an object (target) with a mock object.

    0 讨论(0)
提交回复
热议问题