我正在为Django REST Framework
应用编写单元测试,并且正在使用factory_boy
创建我的假测试数据。我尝试运行测试时遇到以下错误消息
File "/Users/thomasheatwole/osf-meetings/meetings/conferences/tests.py", line 69, in setUp
contributor = UserFactory()
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 67, in __call__
return cls.create(**kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 594, in create
return cls._generate(True, attrs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 519, in _generate
obj = cls._prepare(create, **attrs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 494, in _prepare
return cls._create(model_class, *args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/django.py", line 181, in _create
return manager.create(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 745, in save_base
update_fields=update_fields, raw=raw, using=using)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 192, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/thomasheatwole/osf-meetings/meetings/submissions/signals.py", line 19, in add_permissions_on_submission_save
submission, submission_contributor, conference_admin, approval)
File "/Users/thomasheatwole/osf-meetings/meetings/submissions/permissions.py", line 167, in set_unapproved_submission_permissions
approval, submission_contributor)
File "/Users/thomasheatwole/osf-meetings/meetings/approvals/permissions.py", line 62, in add_approval_permissions_to_submission_contributor
assign_perm("approvals.delete_approval", submission_contributor, approval)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/guardian/shortcuts.py", line 92, in assign_perm
return model.objects.assign_perm(perm, user, obj)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/guardian/managers.py", line 43, in assign_perm
obj_perm, created = self.get_or_create(**kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 467, in get_or_create
return self._create_object_from_params(lookup, params)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 499, in _create_object_from_params
obj = self.create(**params)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/guardian/models.py", line 39, in save
content_type = ContentType.objects.get_for_model(self.content_object)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 55, in get_for_model
opts = self._get_opts(model, for_concrete_model)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 32, in _get_opts
model = model._meta.concrete_model
AttributeError: 'NoneType' object has no attribute '_meta'
由于我对后端结构的理解并不是很好,所以我几乎不知道发生了什么。这是工厂:
class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
class ConferenceFactory(factory.DjangoModelFactory):
class Meta:
model = Conference
class ApprovalFactory(factory.DjangoModelFactory):
class Meta:
model = approvalModels.Approval
class SubmissionFactory(factory.DjangoModelFactory):
class Meta:
model = submissionModels.Submission
我在这里打电话给他们:
def setUp(self):
self.user1 = UserFactory(
username = 'Leo',
id = '99'
)
self.user2 = UserFactory(
username = 'LeoLeo'
)
self.conference = ConferenceFactory(
admin = self.user1
)
self.submission1 = SubmissionFactory(
conference = self.conference,
contributor = UserFactory()
)
self.submission2 = SubmissionFactory(
conference = self.conference,
contributor = UserFactory()
)
如果您查看错误消息,则会特别抱怨contributor = UserFactory()
让我知道是否有一个简单的解决方案,或者甚至对正在发生的事情的一些解释会很好。
非常感谢!
这是文件:
答案 0 :(得分:0)
您需要先在工厂定义中添加正确的关系(SubFactory
)。
请仔细阅读本部分:
http://factoryboy.readthedocs.io/en/latest/recipes.html#copying-fields-to-a-subfactory