简体   繁体   English

Python,Django单元测试。 RequestFactory测试不起作用

[英]Python, Django Unit testing. RequestFactory test doesnt work

I have this view that I want to test 我有这个观点,我想测试

def change_item(request):
    name_change = request.POST.get('name_change')
    cost_change = request.POST.get('cost_change')
    category_change = request.POST.get('category_change')
    product_id = request.POST.get('product_id')
    category_name = Category.objects.get(name = category_change)
    product = Product.objects.get(id__exact = product_id)
    product.name = name_change
    product.category = category_name
    product.price = cost_change
    product.save()
    return HttpResponse()

And I wrote a test for It,but it doesn't work (I want to test request) 我为它写了一个测试,但它不起作用(我想测试请求)

from django.test import RequestFactory
from django.test import TestCase
from views import change_item
from models import Product
from django.utils import unittest


class TestChange_item(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

    def change_item_test(self):
        # Create an instance of a GET request.
        request = self.factory.get('main/views')
        # Test my_view() as if it were deployed at /customer/details
        response = change_item(request)
        self.assertEqual(response.status_code, 200)

When I run it I get this in console 当我运行它时,我在控制台中得到它

ERROR: change_item_test (src.main.tests.TestChange_item)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/volodia/work/shopping-helper/src/main/tests.py", line 19, in change_item_test
response = change_item(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py", line 24, in _wrapped_view
if test_func(request.user):
AttributeError: 'WSGIRequest' object has no attribute 'user'

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

What can I do with it ? 我该怎么办? Or there is some different way of testing requests ? 或者有一些不同的测试请求方式?

If you look at the documentation you will see that you need to manually add a user to the request . 如果查看文档,您将看到需要手动将用户添加到请求中 It doesn't do this for you automatically. 它不会自动为您执行此操作。

def change_item_test(self):
    request = self.factory.get('main/views')
    request.user = User.objects.create_user(username="bobalong")

Adding a user like this simulates making a logged in request. 添加这样的用户可以模拟登录请求。 If this is NOT what you want then you should create an AnonymousUser instead. 如果这不是您想要的,那么您应该创建一个AnonymousUser In general the error you were getting should provide you with everything you need to know. 一般来说,你得到的错误应该为你提供你需要知道的一切。 It is explicitly telling you that the test is failing because the request doesn't have a user . 它明确告诉您测试失败,因为请求没有user Adding a real user or adding a mock user object to the request is the answer. 添加真实用户或向请求添加模拟用户对象就是答案。

Also, you seem to be testing a view that will only work with a POST request. 此外,您似乎正在测试仅适用于POST请求的视图。 It's going to fail because request.POST does not have the correct dictionary keys. 它会失败,因为request.POST没有正确的字典键。 You can create a POST request by doing 您可以通过执行创建POST请求

factory.post('main/views', {'name_change': 'some name'})`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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