Joseph Adam
Joseph Adam

Reputation: 1642

Pass ID of object from django model

Here is my test

def someTest(self):

        # create an object 
        sampleModel.objects.create(unique_id='999999')

        # add code here to pass the sampleModel object created above to view:page1

        # Make sure the sampleModel object created appears in page 1
        response = self.client.get(reverse('view:page1'))  # check that above created mlab appears
        self.assertEqual(response.status_code, 200)

How can I modify my test to check that the sampleModel object created appears in view:page1 ? The goal of the test to check if created objects appear in page1.

Here is my urls.py

path('<int:pk>/', views.sampleView.as_view(), name='page1'),

Upvotes: 1

Views: 230

Answers (1)

Ben Boyer
Ben Boyer

Reputation: 1234

You can check if the data get inside the template looks as they should looks like:

self.assertContains(response, '999999')
self.assertContains(response, 'another field value')

Or check directly the context returned by the view itself:

self.assertEqual(response.context['object_name']['field_name'], 'value_of_the_field'))

Edit here is the code:

def someTest(self):

        # create an object 
        sample = sampleModel.objects.create(unique_id='999999')

        # add code here to pass the sampleModel object created above to view:page1

        # Make sure the sampleModel object created appears in page 1
        response = self.client.get(reverse('view:page1', kwargs={'pk':sample.pk}))  
        # check that above created mlab appears
        self.assertEqual(response.status_code, 200)
        #other stuff to test depending of what your view return.

Upvotes: 1

Related Questions