Reputation: 673
I have a Django application and the custom user - want to test it (do unit testing). My custom user is emailuser, which consists of e-mail and password fields. I want to set up something of such national mplayer, give it a field and a password. But my code below does not work.
settings.py
AUTH_USER_MODEL = 'custom_user.EmailUser'
My code in 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]')
Upvotes: 2
Views: 2637
Reputation: 333
Well, try this:
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 methods.
user = User.objects.create(**params)
self.assertEqual(user.pk, user.id)
Upvotes: 6
Reputation: 45585
You should use the create_user()
method instead of just create()
. Also you should check the email
field, not the user
itself.
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]')
Upvotes: 2