Thursday, July 27, 2017

Testing UpdateView in Django using Unittest

I am writing a test case for my Django application named "blog". I am getting the following errors :-

1. I am testing my view function i.e PostUpdateview the code is 

class PostUpdateView(LoginRequiredMixin, UserOwnerMixin, UpdateView):
    model = Post
    form_class = PostModelForm
    template_name = 'posts/update_view.html'

    def get_context_data(self, *args, **kwargs):
        context = super(PostUpdateView, self).get_context_data(*args, **kwargs)
        context['title'] = self.object.title
        print(context['title'])
        return context

    def form_valid(self, form):
        updated_post = form.save(commit=False)
        form.instance.user = self.request.user
        updated_post.save()
        self.object = updated_post
        return super(PostUpdateView, self).form_valid(form)

    def get_success_url(self):
        post_slug = self.slug
        print(post_slug)
        return reverse('posts:detail', kwargs={'slug': post_slug}

The test code for the same is :-
class TestPostUpdateView(BaseUserTestCase):

    def setUp(self):
        # call BaseUserTestCase.setUp()
        super(TestPostUpdateView, self).setUp()
        # Instantiate the view directly. Never do this outside a test!
        self.view = PostUpdateView()
        # Generate a fake request
        request = self.factory.get('/test-title/update')
        # Attach the user to the request
        request.user = self.user
        # Attach the request to the view
        self.view.request = request

    def test_get_success_url(self):
        # Expect: '/users/testuser/', as that is the default username for
        #   self.make_user()
        self.assertEqual(
            self.view.get_success_url(),
            '/posts/test-title/'
        )

    def test_get_object(self):
        # Expect: self.user, as that is the request's user object
        self.assertEqual(
            self.view.get_object(),
            self.title
        )
The traceback is :-
ERROR: test_get_object (blog.test.test_views.TestPostUpdateView)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/media/abhi/300Gb/Office/itoucan_2017/itoucan/blog/test/test_views.py", line 96, in test_get_object
    self.view.get_object(),
  File "/media/abhi/300Gb/Office/venv/lib/python3.5/site-packages/django/views/generic/detail.py", line 35, in get_object
    pk = self.kwargs.get(self.pk_url_kwarg)
AttributeError: 'PostUpdateView' object has no attribute 'kwargs'

======================================================================
ERROR: test_get_success_url (blog.test.test_views.TestPostUpdateView)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/media/abhi/300Gb/Office/itoucan_2017/itoucan/blog/test/test_views.py", line 89, in test_get_success_url
    self.view.get_success_url(),
  File "/media/abhi/300Gb/Office/itoucan_2017/itoucan/blog/views.py", line 45, in get_success_url
    post_slug = self.slug
AttributeError: 'PostUpdateView' object has no attribute 'slug'

What might be the problem ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/842d7f3f-f746-4724-a45e-9c81edd3f248%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment