亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django:測試由url模式觸發的基于TemplateView的視圖?

Django:測試由url模式觸發的基于TemplateView的視圖?

慕虎7371278 2022-09-06 18:01:39
假設我有以下網址path('clients/by_<str:order>', BrowseClients.as_view(), name='browse_clients')及其相應的視圖@method_decorator(login_required, name='dispatch')class BrowseClients(TemplateView):    template_name = "console/browse_clients.html"    def get_context_data(self, **kwargs):        context = super().get_context_data(**kwargs)        context['clients'] = Client.objects.filter(            owner=self.request.user.id).order_by(self.kwargs["order"])        context['form'] = AddClientForm()        return context如何測試?contextclass TestBrowseClientsView(TestCase, GeneralViewTest):    fixtures = ['users.yaml', 'clients.yaml']    def setUp(self):        self.request = RequestFactory().get('/console/clients/by_inscription')        self.request.user = User.objects.get(pk=1)    def test_return_client_ordered_by_inscription_date(self):         view = BrowseClients()        view.setup(self.request)        context = view.get_context_data()天真地,我以為這會根據中發現的模式“喂養”相關內容。但事實似乎并非如此。view.setup(self.request).get_context_data()kwargsself.request======================================================================ERROR: test_return_client_ordered_by_inscription_date (console.tests.TestBrowseClientsView)----------------------------------------------------------------------Traceback (most recent call last):  File "/usr/src/jengu/console/tests.py", line 164, in test_return_client_ordered_by_inscription_date    context = view.get_context_data()  File "/usr/src/jengu/console/views.py", line 34, in get_context_data    owner=self.request.user.id).order_by(self.kwargs["order"])KeyError: 'order'----------------------------------------------------------------------為什么會這樣呢?我設法通過和明確地解決了我的問題,但它看起來有點特別:statusorderdef get_context_data(self, status, order, **kwargs):    def test_return_clients_ordered_by_parameter(self):         view = BrowseClients()        view.setup(self.request)        context = view.get_context_data("all", "inscription")在這里提到的不同選項中,哪一個更規范?我是否走錯了路,在定義時顯式使用變量?get_context_data()
查看完整描述

2 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

如果要檢查響應上下文中的內容,首先需要使用響應對象(而您不是,您只是在創建視圖的實例,而不是獲取視圖生成的響應)。我不知道,但我相信你會找到如何使我的答案適應你的用例。RequestFactory


所以,它會像這樣:


def test_your_context(self):

    user = User.objects.get(pk=1)

    self.client.force_login(user) # because of the login_required decorator

    response = self.client.get(reverse("browse_clients"))

    assert response.context['your_context_key'] == "Anything you want to check"

只需做幾件事就可以進一步說明:

  • 你的方法的定義對我來說似乎沒問題,get_context_data

  • 如果您使用基于類的視圖,如果您要檢查用戶是否登錄,我會建議您也使用基本視圖(LoginRequiredMixin)

  • 你給你的網址起了個名字,所以只是使用它,而不是寫它的原始形式(這就是我在答案中所做的)。


查看完整回答
反對 回復 2022-09-06
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

如果使用測試客戶端,它將負責運行中間件和初始化視圖。


當您直接使用和調用視圖時,URL 處理程序不會運行,因此由您來傳遞 kwargs。setup()


def test_return_client_ordered_by_inscription_date(self): 

    view = BrowseClients()

    view.setup(self.request, order='inscription')

    context = view.get_context_data()


查看完整回答
反對 回復 2022-09-06
  • 2 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號