简体   繁体   中英

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.

You pass it when you call the view.

view = DeleteHelp.as_view()
response = view(request, pk=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM