[英]Pass ID of object from django model
Here is my test
这是我的
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
?如何修改我的测试以检查创建的
sampleModel
对象是否出现在view:page1
? The goal of the test to check if created objects appear in page1.测试的目标是检查创建的对象是否出现在 page1 中。
Here is my urls.py
这是我的
urls.py
path('<int:pk>/', views.sampleView.as_view(), name='page1'),
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.