第63~66页
我运行代码
python manage.py test
错误消息是
Creating test database for alias 'default'...
FF.
======================================================================
FAIL: test_home_page_can_save_a_POST_request (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/hanminsoo/Documents/TDD_test/TDD_Test/superlists/lists/tests.py", line 33, in test_home_page_can_save_a_POST_request
self.assertEqual(response.content.strip().decode(), expected_html)
AssertionError: '<htm[381 chars] 신규 작업 아이템\n </td>\n </tr>\n [28 chars]tml>' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n'
======================================================================
FAIL: test_home_page_returns_correct_html (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/hanminsoo/Documents/TDD_test/TDD_Test/superlists/lists/tests.py", line 18, in test_home_page_returns_correct_html
self.assertEqual(response.content.decode(), expected_html)
AssertionError: '<htm[381 chars] None\n </td>\n </tr>\n </t[25 chars]l>\n' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n'
----------------------------------------------------------------------
Ran 3 tests in 0.011s
FAILED (failures=2)
Destroying test database for alias 'default'...
这是我的代码 - (home.html)
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="작업 아이템 입력" />
{% csrf_token %}
</form>
<table id="id_list_table">
<tr>
<td>
{{ new_item_text }}
</td>
</tr>
</table>
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
return render(
request,
'home.html',
{
'new_item_text': request.POST.get('item_text')
}
)
test.py
from django.core.urlresolvers import resolve
from django.test import TestCase
from django.http import HttpRequest
from django.template.loader import render_to_string
from lists.views import home_page
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
self.assertEqual(found.func, home_page)
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
expected_html = render_to_string('home.html', request=request)
self.assertEqual(response.content.decode(), expected_html)
self.assertIn(b'<title>To-Do lists</title>', response.content.strip())
def test_home_page_can_save_a_POST_request(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = '신규 작업 아이템'
response = home_page(request)
self.assertIn('신규 작업 아이템', response.content.strip().decode())
expected_html = render_to_string(
'home.html',
request=request
)
self.assertEqual(response.content.strip().decode(), expected_html)
首先,我的解决方案是添加代码&#39;&#39;在views.py
中'<htm[381 chars] None\n </td>\n </tr>\n</t[25 chars]l>\n' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n
1
'new_item_text': request.POST.get('item_text')
已更改
'new_item_text': request.POST.get('item_text', '')
所以,我解决了一个问题
&#39; test_home_page_returns_correct_html&#39;
但我找不到第二个问题......
'<htm[381 chars] 신규 작업 아이템\n </td>\n </tr>\n [28 chars]tml>' != '<htm[381 chars] \n </td>\n </tr>\n </table[21 chars]l>\n'
为什么它要添加&#39;신규작업아이템&#39; ??
请帮助我!