Finished 4.4 On Refactoring

chap4
Jason Zhu 2020-11-07 21:05:22 +11:00
parent 89224c9928
commit 747e0c7009
1 changed files with 4 additions and 11 deletions

View File

@ -12,16 +12,9 @@ class HomePageTest(TestCase):
def test_home_page_returns_correct_html(self): def test_home_page_returns_correct_html(self):
""" """
1. Create an HttpRequest object, which is what Django will see when a user's browser asks for a page 1. Instead of manually creating HttpRequest object and calling view function to get response, pass in URL to `self.client.get()` and get response directly
2. Pass it to `home_page` view, which gives us a response. 3. `.assertTemplateUsed` is the test method that Django Test Case provides. it checks whether template was used to render a response
3. Extract `.content` of the response, which are byte value, and should be decoded into string (HTML format)
4. Check HTML starts and end with <html> tag
5. Want to find <title> tag in the middle
""" """
request = HttpRequest() #1 response = self.client.get('/') #1
response = home_page(request) #2 self.assertTemplateUsed(response, 'home.html') #3
html = response.content.decode('utf8') #3
self.assertTrue(html.startswith('<html>')) #4
self.assertIn('<title>To-Do lists</title>', html.strip()) #5
self.assertTrue(html.strip().endswith('</html>')) #4