Django / Factoryboy - 无法删除测试中的实例
Django / Factoryboy - unable to delete instances in test
我正在为 Django 应用程序编写测试,我 运行 遇到了一个问题,在我应该删除它们之后,删除的对象仍然存在于测试数据库中。
我正在使用以下工厂
class CMSPageFactory(factory.DjangoModelFactory):
class Meta:
model = CMSPage
title = factory.Faker('company')
tenant = factory.SubFactory(TenantFactory)
key = factory.Faker('slug')
protected = False
in_navigation = False
active = True
这是我在运行宁
的考试
def test_example_for_so(self):
page = CMSPageFactory()
page.delete()
self.assertFalse(page)
并引发以下错误:
AssertionError: <CMSPage: Fletcher LLC> is not false
我一定遗漏了一些非常明显的东西,但我终究无法弄清楚是什么。有谁知道我做错了什么?
您确定 page
仍然存在于数据库中吗?
在 django 模型实例(你的工厂应该创建)上调用 delete()
将 删除数据库行,但也不会删除你的本地 python 表示:
https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.delete
Issues an SQL DELETE for the object. This only deletes the object in the database; the Python instance will still exist and will still have data in its fields.
对象已从数据库中删除但仍存在于内存中。来自 Model delete docs:
Issues an SQL DELETE for the object. This only deletes the object in
the database; the Python instance will still exist and will still have
data in its fields. This method returns the number of objects deleted
and a dictionary with the number of deletions per object type
在测试中你可以做的是获取id然后尝试从数据库中获取对象,或者统计数据库中的对象并期望为0。
我正在为 Django 应用程序编写测试,我 运行 遇到了一个问题,在我应该删除它们之后,删除的对象仍然存在于测试数据库中。
我正在使用以下工厂
class CMSPageFactory(factory.DjangoModelFactory):
class Meta:
model = CMSPage
title = factory.Faker('company')
tenant = factory.SubFactory(TenantFactory)
key = factory.Faker('slug')
protected = False
in_navigation = False
active = True
这是我在运行宁
的考试def test_example_for_so(self):
page = CMSPageFactory()
page.delete()
self.assertFalse(page)
并引发以下错误:
AssertionError: <CMSPage: Fletcher LLC> is not false
我一定遗漏了一些非常明显的东西,但我终究无法弄清楚是什么。有谁知道我做错了什么?
您确定 page
仍然存在于数据库中吗?
在 django 模型实例(你的工厂应该创建)上调用 delete()
将 删除数据库行,但也不会删除你的本地 python 表示:
https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.delete
Issues an SQL DELETE for the object. This only deletes the object in the database; the Python instance will still exist and will still have data in its fields.
对象已从数据库中删除但仍存在于内存中。来自 Model delete docs:
Issues an SQL DELETE for the object. This only deletes the object in the database; the Python instance will still exist and will still have data in its fields. This method returns the number of objects deleted and a dictionary with the number of deletions per object type
在测试中你可以做的是获取id然后尝试从数据库中获取对象,或者统计数据库中的对象并期望为0。