How to mock chained function calls in python?

前端 未结 1 538
余生分开走
余生分开走 2021-02-05 08:10

I\'m using the mock library written by Michael Foord to help with my testing on a django application.

I\'d like to test that I\'m setting up my query properly, but I don

相关标签:
1条回答
  • 2021-02-05 08:26

    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.

    For your example,

    self.assertTrue(query_mock.distinct.called)
    

    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)
    
    0 讨论(0)
提交回复
热议问题