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 Django Basics Test Time View Tests

Richard Li
Richard Li
9,751 Points

Testing Error courses.models.DoesNotExist: Course matching query does not exist.

here is my TestCase for step creation:

def test_step_creation(self):
        test = Step.objects.create(
            title='test',
            description='test',
            order=6,
            content='test',
            course=Course.objects.get(pk=1)
        )
        self.assertIn(test, Step.objects.all())

it will always through back an models.DoesNotExist

My assumptions is that TestCase doesn't have any real connection to the database thus the course ForeignKey query will always return Fail here.

Am I right?

if so then how to effectively test in this scenario? if not what is the problem?

Thanks!!!