vendredi 2 juin 2017

Passing kwargs to class based view in unittest

I have test like that:

def test_getting_delete_view_invokes_point_changes_and_returns_status_200(self, point_changes):
        request = RequestFactory().get(reverse('questions:delete-help', kwargs={'pk': 1}))
        view = DeleteHelp.as_view()
        view.kwargs['pk'] = 1
        response = view(request)

And my view function:

class DeleteHelp(DeleteView, LoginRequiredMixin):

    model = Help
    template_name = 'confirm_deletion.html'

    def get_object(self, queryset=None):
        return get_object_or_404(Help, pk=self.kwargs['pk'], author=self.request.user)

    def get_success_url(self):
        point_pk = self.object.answer_to.plan_point.point_of.id
        point_changes(point_obj=self.object.answer_to.plan_point)
        return reverse('plans:display-public', args=[point_pk])

The question is, how am I supposed to pass 'pk' there? I keep getting an error KeywordError 'pk' in get_object method. If I use self.client to access this view then it works (why?), but I want to use RequestFactory.

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire