I am writing an Unit-Test for a Django class-based view.
class ExampleView(ListView):
def get_context_data(self, **kwargs):
context = super(EampleView, self).get_context_data(**kwargs)
## do something else
def get_queryset(self, **kwargs):
return self.get_data()
def get_data(self):
call_external_API()
## do something else
The key issue is that call_external_API()
in get_data()
.
When I am writing Unit-test, I don't really want to call external API to get data. First, that will cost my money; second, I can easily test that API in another test file.
I also can easily test this get_data()
method by having an unit-test only for it and mock the output of call_external_API()
.
However, when I test this whole class-based view, I simple will do
self.client.post('/example/url/')
and check the status code and context data to verify it.
In this case, how do I mock this call_external_API()
when I am testing the whole class-based view?
Aucun commentaire:
Enregistrer un commentaire