7.8.1 A Test Class for New List Creation

chap7-new
Jason Zhu 2020-11-11 14:27:11 +11:00
parent 8bf071ae61
commit 24fb7b1e09
1 changed files with 9 additions and 10 deletions

View File

@ -13,22 +13,21 @@ class HomePageTest(TestCase):
response = self.client.get('/') response = self.client.get('/')
self.assertTemplateUsed(response, 'home.html') self.assertTemplateUsed(response, 'home.html')
def test_can_save_a_POST_request(self): def test_only_saves_items_when_necessary(self):
self.client.post('/', data={'item_text': 'A new list item'}) self.client.get('/')
self.assertEqual(Item.objects.count(), 0)
class NewListTest(TestCase):
def test_can_save_a_POST_request(self):
self.client.post('/lists/new', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1) self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first() new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item') self.assertEqual(new_item.text, 'A new list item')
def test_redirects_after_POST(self): def test_redirects_after_POST(self):
response = self.client.post('/', data={'item_text': 'A new list item'}) response = self.client.post('/lists/new', data={'item_text': 'A new list item'})
self.assertRedirects(response=response, expected_url='/lists/the-only-list-in-the-world/')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/lists/the-only-list-in-the-world')
def test_only_saves_items_when_necessary(self):
self.client.get('/')
self.assertEqual(Item.objects.count(), 0)
class ListViewTest(TestCase): class ListViewTest(TestCase):