有点奇怪。我已经从 Django 3.2 升级到 4.0。我的很多测试都失败了,而且在我测试表单提交的结果时它们都失败了。但是,当我使用浏览器对其进行测试时,表单本身可以正常工作。并且所有测试都以完全相同的方式与消息失败AssertionError: The form 'form' in context 166 does not contain the field 'date'
(显然,每个测试中的字段、表单名称和编号都不同)。
我查看了 Django 文档以查看测试表单的方式是否发生了变化,但我没有看到任何可能导致这种情况的提及。
样品测试:
def test_expenses_new_and_edit_ye(self):
""" Submits expense before and after ye date, then again with edit """
self.client.force_login(User.objects.get_or_create(username='testuser')[0])
# Redate the most recent YE to 10 days ago
ye = JournalEntry.objects.filter(type='YE').order_by('-id')[0]
ye.date = (datetime.today() - relativedelta(days=10))
ye.save()
# Try to submit into previous financial year
date = (datetime.today() - relativedelta(days=10)).strftime('%Y-%m-%d')
response = self.client.post(reverse('journal:expenses_new'), {'date':date, 'account': 20, 'expense': 7, 'project': 1, 'store': 'Test store 223', 'amount': 10})
self.assertFormError(response, 'form', 'date', 'Date must be within current financial year')
样本输出:
======================================================================
FAIL: test_expenses_new_and_edit_ye (journal.tests.test_main.ExpensesChecks)
Submits expense before and after ye date, then again with edit
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Philip\CodeRepos\Acacia2\journal\tests\test_main.py", line 1049, in test_expenses_new_and_edit_ye
self.assertFormError(response, 'form', 'date', 'Date must be within current financial year')
File "C:\Users\Philip\CodeRepos\Acacia2\venv\lib\site-packages\django\test\testcases.py", line 517, in assertFormError
self.fail(
AssertionError: The form 'form' in context 166 does not contain the field 'date'