When testing uniqueness validations with shoulda-matcher in RSpec, an error could occur when the an attribute of the class also has a not null constrain on the database.

The validate_uniqueness_of method needs an instance in the database to work, if there’s no instance in the database it creates one with the given attribute but doesn’t set up the rest. It bypasses the rails’ validation but not the database’s.

One simple workabout (using FactoryGirl where the user factory has all the necessary attributes):

1
2
3
4
it 'should validate the uniqueness of email' do
    create(:user)
    should validate_uniqueness_of(:email)
  end

Yup, that’s it.