mercredi 19 décembre 2018

Django testing an update post

I receive this message "feed.models.Post.DoesNotExist: Post matching query does not exist." I believe it to be in the UpdatePost class I dont understand as there is a post created with an id of one. Why is this?

from django.test import TestCase, SimpleTestCase
from django.contrib.auth.models import User
from django.urls import reverse

from feed.models import Post

class Setup_Class(TestCase):

 def setUp(self):
    self.user = User.objects.create_user(username='jtur', email='jtur@accenture.com', password='onion')
    user = User.objects.first()
    Post.objects.create(title='test', content='more testing', author=user)

class PostTests(Setup_Class):

 def test_content(self):
    post = Post.objects.get(id=1)
    expected_post_title = f'{post.title}'
    expected_post_content = f'{post.content}'
    self.assertEquals(expected_post_title, 'test')
    self.assertEquals(expected_post_content, 'more testing')

 def test_post_list_view(self):
    response = self.client.get(reverse('feed-home'))
    self.assertEqual(response.status_code, 200)
    self.assertContains(response, 'more testing')
    self.assertTemplateUsed(response, 'feed/home.html')

class UpdatePost(Setup_Class):

 def test_post_update(self):
    post = Post.objects.get(id=1)
    post.title = "This has been changed"
    expected_post_title = post.title
    self.assertEquals(expected_post_title, 'This has been changed')

Aucun commentaire:

Enregistrer un commentaire