Use “get” methods to help writing more testable code

By | December 23, 2021

Imagine you have the following code

If you were writing unit test against do_something(), you would need to do some complex mocking on User.objects.filter() to return some mock up data.

But this can be easily prevented by refactoring into a separate get_users() method as below:

This way, you can just mock get_users() to return the mock data.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.