需要请求的 Django 表单使测试更难?

Django forms which needs the request , makes testing harder?

我有很多 django 表单,我在其中将请求作为 kwarg 传递。
我刚刚开始深入测试,似乎需要请求作为参数的测试表单使测试更加困难。因为我必须以某种方式创建一个请求,没有它我无法测试我的表单。

那么最好完全避免将请求传递给表单吗?或者另一种解决方法? 我首先这样做的原因是有时我需要 request.user 或 request.session 并根据表格中的信息做一些 cleaning/setting。

更新:
这是一个示例表格:

class OrderForm(forms.ModelForm):
   def __init__(self, *args, **kwargs):
      self.request = kwargs.pop('request')
      self.user = self.request.user

   def clean(self):
       # Here I have some cross session-field validation
       if self.request.session['has_response'] and self.cleaned_data('status') == 'NEW':
            raise ValidationError()

   def save(self, commit=False):
      self.instance.user = self.user
      return super(OrderForm, self).save(commit=True)

   class Meta:
      model = Order
      fields = ('address', 'city', 'status', ) # more fields

查看代码很简单:

form = OrderForm(request.POST, request=request)

Order 模型也有一个带有一些验证逻辑的 clean() 方法。 会话最多在用户登录期间填充。 我需要 session/user 的要点。

但最重要的问题是,考虑到测试此表单的选项,将请求和会话传递给表单是否是一个糟糕的设计?当表单负责保存对象(包括 request.user)时,我发现它更合乎逻辑。但也许我应该尝试在表单和视图之间进行拆分?

如果在clean()方法中需要,将请求传递给表单是可以的。您可以在这样的测试中使用 request/session/user:

from django.test import TestCase, Client
from django.test.client import RequestFactory
from django.contrib.auth.models import AnonymousUser, User
from .views import my_view
from .forms import MyForm
from django.contrib.sessions.middleware import SessionMiddleware

# If Python >= 3.4
from unittest.mock import patch, MagicMock
# Else
from mock import patch, MagicMock

class SimpleTest(TestCase):
    def setUp(self):
        # Create a RequestFactory accessible by the entire class.
        self.factory = RequestFactory()
        # Create a new user object accessible by the entire class.
        self.user = User.objects.create_user(username='username', 
                                 email='email', password='password')

    def test_my_view(self):
        # Create an instance of a GET request.
        request = self.factory.get('/my-url/')

        # Middleware is not supported so simulate a
        # logged-in user by setting request.user.
        request.user = self.user

        # Or add anonymous user to request.
        request.user = AnonymousUser()

        # Test view() at '/my-url/'
        response = my_view(request)
        self.assertEqual(response.status_code, 200)

    @patch('app.models.ModelName.save', MagicMock(name="save"))
    def test_my_form_view_with_factory(self):
        # Set up form data.
        form_data = {'something': 'something'}

        # Create an instance of a POST request.
        request = self.factory.post('/my-form-url/', form_data)

        # Simulate logged-in user
        request.user = self.user

        # Setup session.
        middleware = SessionMiddleware()
        middleware.process_request(request)
        request.session.save()

        # Or you should just be able to do
        request.session['somekey'] = 'test'
        request.session.save()           

        # Get response from form view, and test passing 
        # request/data to form.
        form = MyForm(request=request, data=form_data)
        response = my_form_view(request)

        self.assertTrue(form.is_valid())
        self.assertEqual(response.status_code, 200)

        # If model form you can do 
        self.assertTrue(ModelName.save.called)

    @patch('app.models.ModelName.save', MagicMock(name="save"))
    def test_my_form_view_with_client(self):
        # Use Client instead of RequestFactory.
        self.client = Client()

        # Login with Client.
        self.client.login(username='username', password='password')

        # Set up form data.
        form_data = {'something': 'something'}

        # Get/set session.
        session = self.client.session
        session['somekey'] = 'test'
        session.save()

        # Get response with Client.
        response = self.client.post('/my-form-url/', form_data)
        self.assertEqual(response.status_code, 200)

        # If model form you can do 
        self.assertTrue(ModelName.save.called)

应该大致了解您可以做什么,而不是具体测试。