[英]model_mommy - user to assignment relationship
I'm finally setting up testing for my Django app, but I'm having difficulties getting started. 我终于为我的Django应用设置了测试,但是入门上遇到了困难。 I'm using model_mommy
to create dynamic data for my tests, but have the following problem: 我正在使用model_mommy
为测试创建动态数据,但是存在以下问题:
The view I'm testing is supposed to show me all the assignments
a particular user has to complete. 我正在测试的视图应该向我显示特定用户必须完成的所有assignments
。 To test this, I want to create 500 assignments, log into the app and check if they are shown. 为了测试这一点,我想创建500个作业,登录到应用程序并检查是否显示了它们。 So far I have the following test cases: 到目前为止,我有以下测试案例:
class TestLogin(TestCase):
def setUp(self):
self.client = Client()
user = User.objects.create(username='sam')
user.set_password('samspassword')
user.save()
def test_login(self):
self.client.login(username='sam', password='samspassword')
response = self.client.get('/')
print (response.content)
self.assertEqual(response.status_code, 200)
and 和
class TestShowAssignments(TestCase):
def setUp(self):
user_recipe = Recipe(User, username='sam', password="samspassword")
self.assignment = Recipe(Assignment,
coders = related(user_recipe))
self.assignments = self.assignment.make(_quantity=500)
def test_assignments(self):
self.assertIsInstance(self.assignments[0],Assignment)
self.assertEqual(len(self.assignments),500)
The first test passes fine and does what it should: TestLogin
logs the user in and shows his account page. 第一个测试通过了,并完成了应TestLogin
措施: TestLogin
登录用户并显示其帐户页面。 The trouble starts with TestShowAssignments
, which creates 500 assignments but if I look at the assignments with print (self.assignments[0].coders)
, I get auth.User.None
. 麻烦始于TestShowAssignments
,它创建了500个分配,但是如果我查看带有print (self.assignments[0].coders)
的分配print (self.assignments[0].coders)
, auth.User.None
得到auth.User.None
。 So it doesn't add the user I defined as a relation to the assignments. 因此,它不会将我定义的用户添加到分配中。 What might be important here is that the coders
field in the model is a m2m field, which I tried to address by using related
, but that doesn't seem to work. 在这里重要的是,模型中的coders
字段是一个m2m字段,我尝试使用related
来解决这个问题,但这似乎不起作用。
What also doesn't work is logging in: if I use the same code I use for logging in during TestLogin
in TestShowAssignments
, I can't log in and see the user page. 登录也不行:如果我使用与TestLogin
中的TestShowAssignments
登录时所使用的相同的代码,则无法登录并查看用户页面。
So, my question: How do I use model_mommy
to create Assignments and add them to a specific user, so that I can log in as that user and see if the assignments are displayed properly? 因此,我的问题是:如何使用model_mommy
创建工作分配并将其添加到特定用户,以便我可以以该用户身份登录并查看工作分配是否正确显示?
Do you want 500 Assignments that all have User "sam" as a single entry in the 'coders' field? 您是否希望500个分配都在“编码器”字段中将用户“ sam”作为单个条目? If so, try: 如果是这样,请尝试:
from model_mommy import mommy
...
class TestShowAssignments(TestCase):
def setUp(self):
self.user = mommy.make(User, username='sam', password='samspassword')
self.assignments = mommy.make(Assigment, coders=[self.user], _quantity=500)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.