我正在为Django应用程序编写测试,目前遇到以下问题:
我有一个(抽象)模型:
class HeroContent(models.Model):
title = models.CharField(
max_length=100, blank=True, null=True,
default=None)
subtitle = models.CharField(
max_length=255, blank=True, null=True,
default=None)
class Meta:
abstract = True
为此,我创建了以下工厂:
^{pr2}$关于如何处理抽象模型,我咨询了documentation,但是当我运行以下测试时:
class HeroContentFactoryTest(TestCase):
def test_init(self):
hero_content = HeroContentFactory()
引发以下错误:
FactoryError: Cannot generate instances of abstract factory HeroContentFactory; Ensure HeroContentFactory.Meta.model is set and HeroContentFactory.Meta.abstract is either not set or False.
但这似乎与官方文件中建议的路线背道而驰, 上面说什么时候
If a DjangoModelFactory relates to an abstract model, be sure to declare the DjangoModelFactory as abstract
正在从工厂中删除abstract = True
设置
引发以下错误:
AttributeError: 'NoneType' object has no attribute 'create'
在抽象模型上调用.create
当然会失败,但现在我想知道使用factory测试这类模型的正确方法是什么-尤其是文档中建议的sind对我不起作用。在
有人知道我做错了什么吗?在
您需要从
HeroContentFactory
继承到HeroContentConcreteFactory
,这将绑定到HeroContent
的子类,这将是一个具体的模型。不能从抽象模型或抽象工厂实例化。在相关问题 更多 >
编程相关推荐