Since Class based Generic Views in Django involve some work by the framework I find very hard to work with them in a TDD style. Now I use the TestClient to access the view from the http mocked stack, but I would prefer to properly unittest specific methods (es. overrides of get_object and get_queryset ) before ‘functional’ testing with the TestClient.
Is there a ( quick ) way to obtain a proper instance of a ClassView to perform unit test on it?
Generally, that would include creating a request via the
RequestFactoryand instantiating the view class with keyword arguments. Afterwards, you can call any of the view methods and evaluate the result, passing any required arguments.I’d recommend that you review the base
Viewclass, specifically the__init__,as_viewanddispatchmethods. They’re crucial to understanding how the framework interacts with view objects.The most important bit to notice there is that view methods expect to be called during a request-response process, so they’re allowed to rely upon
self.request,self.argsandself.kwargsto be present before they’re called, so make sure you’ve got that covered.