How TDD can be applied to Django Class based Generic Views?

前端 未结 3 1792
误落风尘
误落风尘 2021-02-02 10:13

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

3条回答
  •  余生分开走
    2021-02-02 11:05

    Generally, that would include creating a request via the RequestFactory and 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 View class, specifically the __init__, as_view and dispatch methods. 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.args and self.kwargs to be present before they're called, so make sure you've got that covered.

提交回复
热议问题