我正在尝试为多对多关系创建模型单元测试。目的是检查表成分中是否保存了正确的类别。
class IngredientModelTest(TestCase):
def test_db_saves_ingredient_with_category(self):
category_one = IngredientsCategory.objects.create(name='Food')
first_Ingredient = Ingredient.objects.create(name='Apple')
first_Ingredient.categories.add(category_one)
category_two = IngredientsCategory.objects.create(name='Medicine')
second_Ingredient = Ingredient.objects.create(name='Antibiotics')
second_Ingredient.categories.add(category_two)
first_ = Ingredient.objects.first()
self.assertEqual('Apple', first_.name)
self.assertEqual(first_.categories.all(), [category_one])
self.assertEqual(first_, first_Ingredient)
因为self.asserEqual(first_.categories.all(), [category_one])
在倒数第二行,我得到了这个奇怪的断言:
AssertionError: [<IngredientsCategory: Food>] != [<IngredientsCategory: Food>]
我尝试了许多其他不同的方法,但都没有奏效。有没有人认为我怎样才能获得first_.categories.all()
将它与其他东西进行比较的信息?