unit testing - How do I test Django views when they don't return values -


i'm new django. i've worked through tutorials, written models, views, forms etc. don't understand how can tested since either nothing returned, or whatever returned tightly bound doesn't make sense in terms of test.

for example here 2 view classes:

class listblogpostview(listview):     model = blogpost     template_name = 'app/blogpost_list.html'   class createblogpostview(createview):     model = blogpost     template_name = 'app/blogpost_edit.html'     form_class = blogpostform      def get_success_url(self):         return reverse('blogpost-list')      def get_context_data(self, **kwargs):         context = super(createblogpostview, self).get_context_data(**kwargs)         context['action'] = reverse('blogpost-new')         return context 

the first, listblogpostview, doesn't return anything. how can ever check working correctly?

the second has couple of functions return values not things can test assert

how ever use tdd approach django?

i'm used using nunit , ms 'unit' tests in visual studio, mocking etc

actually, django's generic class-based views not called generic no reason.

you can view source of both listview , createview.


the listview has get method handler:

class baselistview(multipleobjectmixin, view):     """a base view displaying list of objects."""     def get(self, request, *args, **kwargs):         self.object_list = self.get_queryset()         allow_empty = self.get_allow_empty()          if not allow_empty:            # ...          context = self.get_context_data()         return self.render_to_response(context) 

which returns valid django response , can tested in tests.py.


if @ createview, inherits basecreateview (just listview inherits baselistview):

class basecreateview(modelformmixin, processformview):     """     base view creating new object instance.     using base class requires subclassing provide response mixin.     """     def get(self, request, *args, **kwargs):         # ...      def post(self, request, *args, **kwargs):         # ... 

which inherits processformview:

class processformview(view):     """render form on , processes on post."""     def get(self, request, *args, **kwargs):         """handle requests: instantiate blank version of form."""         return self.render_to_response(self.get_context_data())      def post(self, request, *args, **kwargs):         """         handle post requests: instantiate form instance passed         post variables , check if it's valid.         """         form = self.get_form()         if form.is_valid():             return self.form_valid(form)         else:             return self.form_invalid(form) 

the get request result in valid response. see, post method handler here returns either self.form_valid(form) or self.form_invalid(form) depending on form status.

you can see source of these 2 methods in viewmixin:

def form_valid(self, form):     """if form valid, redirect supplied url."""     return httpresponseredirect(self.get_success_url())  def form_invalid(self, form):     """if form invalid, render invalid form."""     return self.render_to_response(self.get_context_data(form=form)) 

both of these methods return valid testable django responses.


in conclusion, both of listblogpostview , createblogpostview can directly tested in tests.py. need have more detailed @ the implementation of django's generic views. the power of open-source!


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -