我不知道如何在django中为扩展用户模型编写测试。
给出profile pattern的模型:
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=100)
我尝试了以下tests.py:
from django.test import TestCase
from profiles.models import Profile
class ProfileTestCase(TestCase):
def setUp(self):
Profile.objects.create(first_name='Robert',
last_name='Mayer',
email='rob@mayer.local',
password='123456',
department='bla')
def test_profile_and_user_attributes(self):
# exists for being a test stub
pass
这将失败:
TypeError: 'password' is an invalid keyword argument for this function
或在评论出密码时:
TypeError: 'first_name' is an invalid keyword argument for this function
我知道我可以基于AbstractUser创建自己的User类,其中上面的测试最有可能开箱即用。但鉴于OneToOneFiled解决方案:如何测试?