samedi 1 juin 2019

How can I test the emailfield validator in django?

I started creating tests for my app. I found some ways to test several things but also wanted to test the email field validator. Who know what can go wrong and when it would come in handy. But basically I just want to know how to do this.

So far I tried several Error codes (integrity, value, validation) but all of them caused an error. Searching on the items is rather difficult for the key words, but I did found some angles. None of them worked and, well, I could look for the wrong clues as well.

This is what I have:

def test_not_an_email(self):
        self.assertRaises(
            ValueError, 
            MyUser.objects.create_user, email='jan@pietnl', password='abcd1234'
            )

But the error message I get is this:

django.db.utils.IntegrityError: UNIQUE constraint failed: accounts_myuser.email

======================================================================
FAIL: test_not_an_email (accounts.tests.UserRegistrationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\xxxxxxxxxxxxxxx\tests.py", line 26, in test_not_an_email
    MyUser.objects.create_user, email='jan@pietnl', password='abcd1234'
AssertionError: ValueError not raised by create_user

----------------------------------------------------------------------
Ran 2 tests in 2.603s

FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...

I thought the test should be correct. What am I doing wrong? Anything that makes me learn to understand this better is much appreciated.

Aucun commentaire:

Enregistrer un commentaire