2017-06-02 52 views
0

我有測試這樣的:傳遞kwargs基於類視圖在單元測試

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) 

而我的看法功能:

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]) 

的問題是,我怎麼傳「PK」有?我在get_object方法中不斷收到錯誤KeywordError 'pk'。如果我使用self.client訪問此視圖,那麼它工作(爲什麼?),但我想使用RequestFactory。

任何幫助,將不勝感激。

回答

2

您在調用視圖時傳遞它。

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