Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

django testing

Hi,

I'm currently trying to write tests for my django application, but I'm having problems testing generic.DeleteView.

DeleteView test

    def test_comment_delete_view(self):
        """Testing comments deleteview and url"""
        resp = self.client.post(reverse('blog:comment_delete', kwargs={'pk': self.comment.pk}))
        self.assertEqual(resp.status_code, 302)

How would I go from here and test if the comment is in the blogpost detailview?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Untested, but if you have the comment.pk can you check to see if that is still a valid pk?

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

I did try testing if it had been deleted like this:

extension of code above

self.assertIn(self.comment, resp.context['comments'])

but this return Ok which should had been Failed if it had been deleted?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The reference self.comment may still exist as a local object, but be absent from the database. You may need to see if the pk is still valid directly:

I was thinking of testing if comment.pk still exists

assertFalse(Comment.objects.filter(pk=self.comment.pk).exists())