Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django测试-TypeError:类型模型的参数不可iterable_Python_Django_Unit Testing - Fatal编程技术网

Python Django测试-TypeError:类型模型的参数不可iterable

Python Django测试-TypeError:类型模型的参数不可iterable,python,django,unit-testing,Python,Django,Unit Testing,运行python manage.py测试时出现错误: 以下是我的测试: def test_course_list_view(self): resp = self.client.get(reverse('courses:list')) self.assertEqual(resp.status_code, 200) self.assertIn(self.course, resp.context['courses']) self.asse

运行python manage.py测试时出现错误:

以下是我的测试:

    def test_course_list_view(self):
       resp = self.client.get(reverse('courses:list'))
       self.assertEqual(resp.status_code, 200)
       self.assertIn(self.course, resp.context['courses'])
       self.assertIn(self.course2, resp.context['courses'])

    def test_course_detail_view(self):
       resp = self.client.get(reverse('courses:detail', args=[self.course.pk]))
       self.assertEqual(resp.status_code, 200)
       self.assertIn(self.course, resp.context['course'])
以下是我正在测试的观点:

def course_list(request):
   courses = Course.objects.all()
   return render(request, 'courses/course_list.html', {'courses': courses})

def course_detail(request, pk):
   course = get_object_or_404(Course, pk=pk)
   return render(request, 'courses/course_detail.html', {'course': course})
困惑是因为我在test\u course\u list\u视图中没有得到错误,但是test\u course\u detail\u视图确实抛出了错误

assertIn做的是名称所暗示的:它断言参数1在参数2中。但是在对细节视图的测试中,您将resp.context['course']作为参数2传递,它不是列表或容器,而是单个实例

你需要比较两者是否相等,而不是一方在另一方

self.assertEqual(self.course, resp.context['course'])

你能提供整个stacktrace吗?已经解决了
self.assertEqual(self.course, resp.context['course'])