module Shoulda::Matchers::ActiveModel
def allow_mass_assignment_of(value)
it { should allow_mass_assignment_of(:first_name).as(:admin) }
In Rails 3.1 you can check role as well:
it { should allow_mass_assignment_of(:first_name) }
it { should_not allow_mass_assignment_of(:password) }
Ensures that the attribute can be set on mass update.
def allow_mass_assignment_of(value) AllowMassAssignmentOfMatcher.new(value) end
def allow_value(*values)
it { should allow_value('isbn 1 2345 6789 0').for(:isbn) }
it { should_not allow_value('bad').for(:isbn) }
Example:
testing `validates!` and the `:strict => true` validation options.
validation fails rather than adding to the errors collection. Used for
* strict - expects the model to raise an exception when the
the test looks for any errors in errors.on(:attribute).
errors.on(:attribute). Regexp or string. If omitted,
* with_message - value the test expects to find in
Options:
argument list (the remaining arguments are not processed then).
are allowed. Otherwise, the matcher fails at the first bad value in the
multiple values are given the match succeeds only if all given values
Ensures that the attribute can be set to the given value or values. If
def allow_value(*values) if values.empty? raise ArgumentError, 'need at least one argument' else AllowValueMatcher.new(*values) end end
def ensure_exclusion_of(attr)
it { should ensure_exclusion_of(:age).in_range(30..60) }
Example:
translation for :exclusion.
errors.on(:attribute). Regexp or string. Defaults to the
* with_message - value the test expects to find in
* in_range - the range of not allowed values for this attribute
* in_array - the array of not allowed values for this attribute
Options:
Ensure that the attribute's value is not in the range specified
def ensure_exclusion_of(attr) EnsureExclusionOfMatcher.new(attr) end
def ensure_inclusion_of(attr)
it { should ensure_inclusion_of(:age).in_range(0..100) }
Example:
translation for :inclusion.
errors.on(:attribute). Regexp or string. Defaults to the
* with_high_message - value the test expects to find in
translation for :inclusion.
errors.on(:attribute). Regexp or string. Defaults to the
* with_low_message - value the test expects to find in
* in_range - the range of allowed values for this attribute
* in_array - the array of allowed values for this attribute
Options:
Ensure that the attribute's value is in the range specified
def ensure_inclusion_of(attr) EnsureInclusionOfMatcher.new(attr) end
def ensure_length_of(attr)
is_equal_to(9).
it { should ensure_length_of(:ssn).
with_short_message(/not long enough/) }
is_at_least(3).
it { should ensure_length_of(:name).
is_at_most(20) }
is_at_least(6).
it { should ensure_length_of(:password).
Examples:
is_equal_to.
translation for :wrong_length. Used in conjunction with
errors.on(:attribute). Regexp or string. Defaults to the
* with_message - value the test expects to find in
translation for :too_long.
errors.on(:attribute). Regexp or string. Defaults to the
* with_long_message - value the test expects to find in
translation for :too_short.
errors.on(:attribute). Regexp or string. Defaults to the
* with_short_message - value the test expects to find in
* is_equal_to - exact requred length of this attribute
* is_at_most - maximum length of this attribute
* is_at_least - minimum length of this attribute
Options:
string/text columns because it uses a string to check length.
Ensures that the length of the attribute is validated. Only works with
def ensure_length_of(attr) EnsureLengthOfMatcher.new(attr) end
def validate_acceptance_of(attr)
it { should validate_acceptance_of(:eula) }
Example:
translation for :accepted.
errors.on(:attribute). Regexp or string. Defaults to the
* with_message - value the test expects to find in
Options:
accepted.
Ensures that the model cannot be saved the given attribute is not
def validate_acceptance_of(attr) ValidateAcceptanceOfMatcher.new(attr) end
def validate_confirmation_of(attr)
it { should validate_confirmation_of(:password) }
Example:
Ensures that the model's attribute matches confirmation
:nodoc:
def validate_confirmation_of(attr) ValidateConfirmationOfMatcher.new(attr) end
def validate_format_of(attr)
with_message(/is not optional/) }
not_with('12D45').
it { should validate_format_of(:name).
with_message(/is not optional/) }
with('12345').
it { should validate_format_of(:name).
Examples:
* not_with(string to test against)
* with(string to test against)
Defaults to the translation for :invalid.
errors.on(:attribute). Regexp or String.
allow_nil - allows a nil value
allow_blank - allows a blank value
* with_message - value the test expects to find in
Options:
formatted correctly.
Ensures that the model is not valid if the given attribute is not
:nodoc:
def validate_format_of(attr) ValidateFormatOfMatcher.new(attr) end
def validate_numericality_of(attr)
it { should validate_numericality_of(:age).only_integer }
it { should validate_numericality_of(:price) }
Examples:
* only_integer - allows only integer values
translation for :not_a_number.
errors.on(:attribute). Regexp or string. Defaults to the
* with_message - value the test expects to find in
Options:
Ensure that the attribute is numeric.
:nodoc:
def validate_numericality_of(attr) ValidateNumericalityOfMatcher.new(attr) end
def validate_presence_of(attr)
with_message(/is not optional/) }
it { should validate_presence_of(:name).
it { should validate_presence_of(:name) }
Examples:
Defaults to the translation for :blank.
errors.on(:attribute). Regexp or String.
* with_message - value the test expects to find in
Options:
present.
Ensures that the model is not valid if the given attribute is not
def validate_presence_of(attr) ValidatePresenceOfMatcher.new(attr) end
def validate_uniqueness_of(attr)
it { should validate_uniqueness_of(:keyword).case_insensitive }
scoped_to(:first_name, :last_name) }
it { should validate_uniqueness_of(:email).
it { should validate_uniqueness_of(:email).scoped_to(:name) }
it { should validate_uniqueness_of(:keyword).with_message(/dup/) }
it { should validate_uniqueness_of(:keyword) }
Examples:
check case. Off by default. Ignored by non-text attributes.
* case_insensitive - ensures that the validation does not
* scoped_to - field(s) to scope the uniqueness to.
Defaults to the translation for :taken.
errors.on(:attribute). Regexp or String.
* with_message - value the test expects to find in
Options:
it { should validate_uniqueness_of(:email) }
Example:
`validate_uniqueness_of`.
constraints. In that case, you must create a record before calling
around validations, so it will probably fail if there are `NOT NULL`
exists in the database. It simply uses `:validate => false` to get
It uses the first existing record or creates a new one if no record
Ensures that the model is invalid if the given attribute is not unique.
:nodoc:
def validate_uniqueness_of(attr) ValidateUniquenessOfMatcher.new(attr) end