【问题标题】:How I can check in test if response get view with or without form?如果响应获得带有或不带有表单的视图,我如何检查测试?
【发布时间】:2019-03-11 16:01:24
【问题描述】:

就像标题一样,我不知道如何测试响应是否是带有或不带有表单的视图,因为在我看来,用户只有在 request.user.is_authenticated 时才能获取表单。

##views.py

def product_detail(request, id, slug):

    product = get_object_or_404(Product, id=id, slug=slug,)
    comments = product.comments.all()

    if request.user.is_authenticated:
        if request.method == 'POST':
            form = CommentForm(request.POST)
            if form.is_valid():
                new_comment = form.save(commit=False)
                new_comment.product = product
                new_comment.nick = get_object_or_404(User, id=str(request.user.id))
                form.save()
                return redirect('product_detail', id=product.id, slug=product.slug)
        else:
            form = CommentForm()

        return render(request, 'shop/product/Product_detail.html',
                      {'product': product, 'form': form, 'comments': comments})

    else:
        return render(request, 'shop/product/Product_detail.html',
                      {'product': product, 'comments': comments})

我有视图测试来检查视图是否加载,但经过身份验证和未经过身份验证的用户都可以访问此视图,唯一的区别是未经身份验证的用户正在获取没有表单的视图,我不知道如何测试。

##test_views.py

    def test_call_view_loads(self):
        product = Product.objects.get(id=1)
        response = self.client.get(f'/shop/{product.id}/{product.slug}/')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'shop/product/Product_detail.html')

【问题讨论】:

    标签: python django unit-testing


    【解决方案1】:

    您可以从测试客户端返回的response 中获取使用的上下文。

    self.assertTrue('form' in response.context)
    

    但是,这不应该是您测试用户是否登录的方式;仅是否将某些上下文变量传递给模板。

    【讨论】:

    • 是的,我知道,要检查用户是否已登录,我有 self.assertTrue(login) 和 login=self.client.login(...)。我只想在测试中显示,如果用户未通过身份验证,则不会返回表单
    【解决方案2】:

    在您的上下文中找到您的表单类:

    self.assertIsInstance(response.context['form'], CommentForm) #find your form in context
    

    【讨论】:

      【解决方案3】:

      您可以在test_call_view_loads 中使用self.client.login(username='user', password='pass') 来执行此操作,如下所示:

      from django.test.client import Client
      
      def setUp(self):
          self.client = Client()
          self.user = User.objects.create_user('user', 'user@email.com', 'pass')
      
      
      def test_call_view_loads_loggined_user(self):
          product = Product.objects.get(id=1)
          self.client.login(username='user', password='pass')  # login the user
          response = self.client.get(f'/shop/{product.id}/{product.slug}/')
          self.assertEqual(response.status_code, 200)
          self.assertTemplateUsed(response, 'shop/product/Product_detail.html')
          self.assertTrue('form' in response.context)  # for check that is form in context or not
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-05
        • 1970-01-01
        相关资源
        最近更新 更多