2015-04-03 57 views
0

我有一個Django應用程序和自定義用戶 - 想測試它(做單元測試)。我的自定義用戶是emailuser,它由電子郵件和密碼字段組成。我想設置一些這樣的國家mplayer,給它一個字段和一個密碼。但我下面的代碼不起作用。Django自定義用戶測試

settings.py

AUTH_USER_MODEL = 'custom_user.EmailUser' 

我在test.py

from django.test import TestCase 
from django.conf import settings 

class MyTest(TestCase): 
    def setUp(self): 
     self.user = settings.AUTH_USER_MODEL.objects.create('[email protected]', 'testpass') 

    def test_user(self): 
     self.assertEqual(self.user, '[email protected]') 

回答

0

代碼,您應該使用create_user()方法,而不是僅僅create()。你也應該檢查email字段,而不是user本身。

class MyTest(TestCase): 
    def setUp(self): 
     self.user = settings.AUTH_USER_MODEL.objects.create_user(
               '[email protected]', 'testpass') 

    def test_user(self): 
     self.assertEqual(self.user.email, '[email protected]') 
0

好了,試試這個:

from django.test import TestCase 
from django.contrib.auth import get_user_model 

User = get_user_model() 

class CustomUserTestCase(TestCase): 

    def test_create_user(self): 
     # params = depends on your basemanager’s create_user methos. 
     user = User.objects.create(**params) 
     self.assertEqual(user.pk, user.id)