我有这个要测试的观点
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()
我为它写了一个测试,但它不起作用(我想测试请求)
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)
当我运行它时,我在控制台中得到它
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)
我能用它做什么?或者有一些不同的测试请求的方式?