2

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('testuser@test.pl', 'testpass')

    def test_user(self):
        self.assertEqual(self.user, 'testuser@test.pl')
mark
  • 653
  • 1
  • 10
  • 23

2 Answers2

6

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)
vigo
  • 298
  • 3
  • 8
2

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(
                                              'testuser@test.pl', 'testpass')

    def test_user(self):
        self.assertEqual(self.user.email, 'testuser@test.pl')
catavaran
  • 44,703
  • 8
  • 98
  • 85