Reputation: 63
I'm trying to write test for my first Django app and I can't seem to test changes made to the database, when I go through a view. Here's an example of one of my test.
def test_vote(self):
self.set_up()
idea = Idea.objects.get(title="Hello people")
c = Client()
c.login(email="[email protected]", password="baxter")
r = c.post(reverse("ideas:up_vote", kwargs={'pk':idea.pk}), follow=True)
self.assertEqual(r.status_code, 200)
self.assertEqual(idea.votes, 1)
The view is suppose to up_vote an idea object, it's a simple view.
def like_view(request, **kwargs):
idea = Idea.objects.get(pk = kwargs['pk'])
idea.votes += 1
idea.save()
return HttpResponseRedirect(reverse('ideas:list'))
I'm using an SQLite3 database.
if DEBUG:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django',
'USER': env('RDS_USERNAME'),
'PASSWORD': env('RDS_PASSWORD'),
'HOST': env('RDS_HOST'),
'PORT': '5432',
}
}
DEBUG = True also. Every time I run the test nothing it says idea.votes = 0 instead of 1 which is what I'm trying to get. I'm not sure if I'm just going about testing this all wrong or what. Any help would be great thanks.
Upvotes: 0
Views: 83
Reputation: 599530
You need to refresh the vote instance you have in your test method after calling the view.
idea = Idea.objects.get(pk=idea.pk)
self.assertEqual(idea.votes, 1)
Upvotes: 1