I am writing System Tests for my Django app, where I test the complete application via HTTP requests and mock its external dependencies' APIs.
In views.py I have something like:
from external_service import ExternalService
externalService = ExternalService
data = externalService.get_data()
@crsf_exempt
def endpoint(request):
do_something()
What I want is to mock (or stub) ExternalService to return a predefined response when its method get_data()
is called.
The problem is that when I run python manage.py test
, views.py is loaded before my test class. So when I patch the object with a mocked one, the function get_data(
) was already called.
This solution didn't work either.