def test_homepage_returns_correct_html(self): request = HttpRequest() response = views.index(request) self.assertTrue(response.content.startswith(b"\n<!DOCTYPE html>"), response.content.decode()) # I don't know how to make unit test ignore newlines. self.assertIn(b"<title>To-Do lists</title>", response.content) self.assertTrue(response.content.endswith(b"</html>"))
def test_user_can_post(self): request = HttpRequest() request.method = "POST" request.POST["item_text"] = "A new text item" # Make a request response = views.index(request) # Count the number of objects in DB self.assertEqual(Item.objects.all().count(), 1) new_item = Item.objects.all()[0] self.assertEqual( new_item.item, "A new text item", "Expected {text} but got {text2}".format(text="A new text item", text2=new_item.item), ) self.assertIn("A new text item", response.content.decode()) expected_html = render_to_string("chronassist/base.html", {"new_item_text": request.POST["item_text"]}) self.assertEqual(response.content.decode(), expected_html)