These matchers will test most of the validations and associations for your ActiveRecord models.
describe User do it { should validate_presence_of(:name) } it { should validate_presence_of(:phone_number) } %w(abcd 1234).each do |value| it { should_not allow_value(value).for(:phone_number) } end it { should allow_value("(123) 456-7890").for(:phone_number) } it { should_not allow_mass_assignment_of(:password) } it { should have_one(:profile) } it { should have_many(:dogs) } it { should have_many(:messes).through(:dogs) } it { should belong_to(:lover) } end
Ensures that the attribute can be set on mass update.
it { should_not allow_mass_assignment_of(:password) } it { should allow_mass_assignment_of(:first_name) }
Ensures that the attribute can be set to the given value.
Options:
Example:
it { should_not allow_value('bad').for(:isbn) } it { should allow_value("isbn 1 2345 6789 0").for(:isbn) }
Ensure that the attribute‘s value is in the range specified
Options:
Example:
it { should ensure_inclusion_of(:age).in_range(0..100) }
Ensures that the length of the attribute is validated.
Options:
Examples:
it { should ensure_length_of(:password). is_at_least(6). is_at_most(20) } it { should ensure_length_of(:name). is_at_least(3). with_short_message(/not long enough/) } it { should ensure_length_of(:ssn). is_equal_to(9). with_message(/is invalid/) }
Ensures that the has_and_belongs_to_many relationship exists, and that the join table is in place.
it { should have_and_belong_to_many(:posts) }
Ensures the database column exists.
Options:
Examples:
it { should_not have_db_column(:admin).of_type(:boolean) } it { should have_db_column(:salary). of_type(:decimal). with_options(:precision => 10, :scale => 2) }
Ensures that there are DB indices on the given columns or tuples of columns.
Options:
Examples:
it { should have_db_index(:age) } it { should have_db_index([:commentable_type, :commentable_id]) } it { should have_db_index(:ssn).unique(true) }
Ensures that the has_many relationship exists. Will also test that the associated table has the required columns. Works with polymorphic associations.
Options:
Example:
it { should have_many(:friends) } it { should have_many(:enemies).through(:friends) } it { should have_many(:enemies).dependent(:destroy) }
Ensure that the has_one relationship exists. Will also test that the associated table has the required columns. Works with polymorphic associations.
Options:
Example:
it { should have_one(:god) } # unless hindu
Ensures that the attribute cannot be changed once the record has been created.
it { should have_readonly_attributes(:password) }
Ensures that the model cannot be saved the given attribute is not accepted.
Options:
Example:
it { should validate_acceptance_of(:eula) }
Ensures that the model is not valid if the given attribute is not formatted correctly.
Options:
Examples:
it { should validate_format_of(:name). with('12345'). with_message(/is not optional/) } it { should validate_format_of(:name). not_with('12D45'). with_message(/is not optional/) }
Ensure that the attribute is numeric
Options:
Example:
it { should validate_numericality_of(:age) }
Ensures that the model is not valid if the given attribute is not present.
Options:
Examples:
it { should validate_presence_of(:name) } it { should validate_presence_of(:name). with_message(/is not optional/) }
Ensures that the model is invalid if the given attribute is not unique.
Internally, this uses values from existing records to test validations, so this will always fail if you have not saved at least one record for the model being tested, like so:
describe User do before(:each) { User.create!(:email => 'address@example.com') } it { should validate_uniqueness_of(:email) } end
Options:
Examples:
it { should validate_uniqueness_of(:keyword) } it { should validate_uniqueness_of(:keyword).with_message(/dup/) } it { should validate_uniqueness_of(:email).scoped_to(:name) } it { should validate_uniqueness_of(:email). scoped_to(:first_name, :last_name) } it { should validate_uniqueness_of(:keyword).case_insensitive }