且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何模拟python中的链接函数调用?

更新时间:2021-07-31 03:52:05

每个模拟对象都保持在模拟对象上当它被调用时返回。您可以使用您的模拟对象的return_value属性来保留它。

Each mock object holds onto the mock object that it returned when it is called. You can get a hold of it using your mock object's return_value property.

对于您的示例,

self.assertTrue(query_mock.distinct.called)

distinct不是调用你的模拟,它被称为你的模拟的过滤器的返回值,所以你可以断言通过这样做来调用distinct:

distinct wasn't called on your mock, it was called on the return value of the filter method of your mock, so you can assert that distinct was called by doing this:

self.assertTrue(query_mock.filter.return_value.distinct.called)