我正在尝试编写一些单元测试,并为此创建了一些虚拟对象。我有一个speaker
部分看起来像这样的模型:
class speaker(models.Model):
name = models.CharField(max_length=255)
email = models.CharField(max_length=255, primary_key=True)
...
和一个talk
部分看起来像这样的类:
class talk(models.Model):
title = models.CharField(max_length = 255)
...
speakers = models.ManyToManyField('speaker')
...
我的测试设置如下所示:
class TalkTest(TestCase):
def setUp(self):
speaker.objects.create(
name = "John Smith",
email = "john@john.com"
)
talk.objects.create(
title = "Writing unit tests",
summary = "How to write unit tests",
speakers = "john@john.com"
)
def test_talk_URLs(self):
testSpeak = speaker.objects.get(name="John Smith")
testTalk = talk.objects.get(title="Writing unit tests")
self.assertEqual(testSpeak.email, "john@john.com")
但是我收到以下回溯错误
======================================================================
ERROR: test_talk_URLs (core.tests.TalkTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/rony/Documents/rehab-lightning-talks/core/tests.py", line 43, in setUp
speakers = "1"
File "/home/rony/Documents/rehab-lightning-talks/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 149, in create
return self.get_query_set().create(**kwargs)
File "/home/rony/Documents/rehab-lightning-talks/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 414, in create
obj = self.model(**kwargs)
File "/home/rony/Documents/rehab-lightning-talks/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 415, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'speakers' is an invalid keyword argument for this function
----------------------------------------------------------------------
我没有正确引用外键吗?