我有以下观点:
from reports_system.apps.reports.forms import UploadReportForm
from reports_system.apps.reports.models import Reports
class IndexReports(ListView):
""""""
model = Reports
context_object_name = 'reports_list'
template_name = 'excel_reports.html'
def get_context_data(self, **kwargs):
context = super(IndexReports, self).get_context_data(**kwargs)
# Add additional context variables
context['UploadReportForm'] = UploadReportForm
return context
这是我的考验:
class ReportsListPageTest(TestCase):
def test_reports_url_resolves_to_reports_list_view(self):
found = resolve('/reports/')
self.assertEqual(found.func.__name__, IndexReports.as_view().__name__)
def test_reports_list_page_returns_correct_html(self):
response = self.client.get('/reports/')
expected_html = render_to_string(
'excel_reports.html',
{'UploadReportForm': UploadReportForm, 'reports_list': Reports.objects.all()}
)
self.assertEqual(response.content.decode(), expected_html)
错误:
in test_reports_list_page_returns_correct_html
self.assertEqual(response.content.decode(), expected_html)
AssertionError: '\n\n[18671 chars] <input type=\'hidden\' name=\'csrfmiddlew[10690 chars]l>\n' != '\n\n[18671 chars] \n [10593 chars]l>\n'
如果我打印response.content.decode()
和expected_html
,它们在视觉上看起来一样。
为什么我有这个错误?
如果使用RequestFactory重写它,测试工作正常,但是我想使用更简单的代码。