1

I have the following chained usage I'd like to mock

Example.objects.order_by(order_by).filter(filters).all()[:page + 1]

It's quite easy to mock all methods involved, but I got no idea how to mock the slicing, given that it's not an "actual" python slice, it translates into LIMIT/OFFSET.

How can this be mocked?

Sebastialonso
  • 1,437
  • 18
  • 34
  • 2
    That's quite an awkward thing to mock, how much setup you'll have is a test smell. Maybe push that down behind a facade to make it easier to mock (and to be mocking something you own). That the query works overall can be tested at the integration rather than unit level. – jonrsharpe Aug 03 '20 at 16:49
  • I guess you can define `__getitem__` on the mock object. See [this answer](https://stackoverflow.com/a/16033058/3486675). – D Malan Aug 03 '20 at 21:29

0 Answers0