How to call self in a mock method of an object in Python?

后端 未结 2 1942
自闭症患者
自闭症患者 2020-12-10 01:49

I try to test some codes which don\'t return anything but save the result to the DB. By mocking the save method, I wish to check whether things have been processed correctly

2条回答
  •  时光说笑
    2020-12-10 02:25

    You need autospec=True

    def mock_save(self):
        assert self.attr == 'dest_val'
    with mock.patch.object(Item, "save", autospec=True) as save:
        save.side_effect = mock_save
        func_to_call()
    

提交回复
热议问题