为什么在测试中渲染的htmls不相等?

时间:2015-12-18 20:26:30

标签: django django-testing

我有以下观点:

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重写它,测试工作正常,但是我想使用更简单的代码。

0 个答案:

没有答案