[x] Add validation that email is a required field.
[x] Add validation that first_name is a required field.
[x] Add validation that last_name is a required field.
[x] Add length validation(maximum: 50) for first_name and last_name field.
[x] Add unique index for email.
[x] Add a not-null constraint to email, first_name, and last_name column.
Add unit test for User model that all the validations mentioned above are met.
Tests should cover the following assertions(Use Minitest):
[x] user should be valid.
[x] first name should be present.
[x] last name should be present.
[x] email should be present.
[x] first name should be of valid length.
[x] last name should be of valid length.
[x] email should be unique.
[x] email address should be saved as lowercase.
[x] email validation should accept valid addresses.
[x] email validation should reject invalid addresses.
if a user is created with email SAM@example.com then another user with email sam@example.com can't be created.
Note:
If you make changes to the model then update the tests according to the changes.
We write model tests to check all the model validations are working or not. To check validations, we write both positive and negative tests.
Positive tests - When we create a model instance with all the valid attributes, we should be able to create it without any errors.
Negative tests - Here we test validation errors. When we try to create a model instance with invalid attributes, it should not be created. To test it we should check that on passing invalid values, model validations should trigger with proper errors.
Write tests in the method style.
See the below examples
Tests should cover the following assertions(Use Minitest):
Note:
def test_user_should_be_valid assert @user.valid? end
def test_invalid_user @user.first_name = "" assert_not @user.valid? assert_equal ["First name can't be blank"], @user.errors.full_messages end