Answer the question
In order to leave comments, you need to log in
How to check for changes in db when testing django view?
Hello!
I'm trying to write a test for django view.
There are some model changes being made in the view and I would like to check if they happened in the end.
For example:
# models.py
class Post(models.Model):
title = models.CharField(max_length=255)
# view.py
def index(request):
post = Post.objects.get(pk=1)
post.title = "Second post"
post.save()
return HttpResponse(post.title)
# tests.py
class BlogTest(TestCase):
def test_post(self):
post = Post.objects.create(title="Hello World")
print "Title before request:", post.title
resp = self.client.get('/')
print "From view:", resp.content
print "Title after request:", post.title
➜ djangotests ./manage.py test
Creating test database for alias 'default'...
Title before request: Hello World
From view: Second post
Title after request: Hello World
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question