Module: Shoulda::ActiveModel::Matchers
- Included in:
- Macros, Test::Unit::TestCase
- Defined in:
- lib/shoulda/active_model/matchers.rb,
lib/shoulda/active_model/matchers/validation_matcher.rb,
lib/shoulda/active_model/matchers/allow_value_matcher.rb,
lib/shoulda/active_model/matchers/ensure_length_of_matcher.rb,
lib/shoulda/active_model/matchers/validate_format_of_matcher.rb,
lib/shoulda/active_model/matchers/ensure_inclusion_of_matcher.rb,
lib/shoulda/active_model/matchers/validate_presence_of_matcher.rb,
lib/shoulda/active_model/matchers/validate_acceptance_of_matcher.rb,
lib/shoulda/active_model/matchers/validate_uniqueness_of_matcher.rb,
lib/shoulda/active_model/matchers/validate_numericality_of_matcher.rb
Overview
Matchers for your ActiveModel models
These matchers will test most of the validations and associations for your ActiveModel 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) }
end
Defined Under Namespace
Classes: AllowValueMatcher, EnsureInclusionOfMatcher, EnsureLengthOfMatcher, ValidateAcceptanceOfMatcher, ValidateFormatOfMatcher, ValidateNumericalityOfMatcher, ValidatePresenceOfMatcher, ValidateUniquenessOfMatcher, ValidationMatcher
Instance Method Summary collapse
-
#allow_value(value) ⇒ Object
Ensures that the attribute can be set to the given value.
-
#ensure_inclusion_of(attr) ⇒ Object
Ensure that the attribute’s value is in the range specified.
-
#ensure_length_of(attr) ⇒ Object
Ensures that the length of the attribute is validated.
-
#validate_acceptance_of(attr) ⇒ Object
Ensures that the model cannot be saved the given attribute is not accepted.
-
#validate_format_of(attr) ⇒ Object
Ensures that the model is not valid if the given attribute is not formatted correctly.
-
#validate_numericality_of(attr) ⇒ Object
Ensure that the attribute is numeric.
-
#validate_presence_of(attr) ⇒ Object
Ensures that the model is not valid if the given attribute is not present.
-
#validate_uniqueness_of(attr) ⇒ Object
Ensures that the model is invalid if the given attribute is not unique.
Instance Method Details
#allow_value(value) ⇒ Object
Ensures that the attribute can be set to the given value.
Options:
-
with_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. If omitted, the test looks for any errors inerrors.on(:attribute)
.
Example:
it { should_not allow_value('bad').for(:isbn) }
it { should allow_value("isbn 1 2345 6789 0").for(:isbn) }
16 17 18 |
# File 'lib/shoulda/active_model/matchers/allow_value_matcher.rb', line 16 def allow_value(value) AllowValueMatcher.new(value) end |
#ensure_inclusion_of(attr) ⇒ Object
Ensure that the attribute’s value is in the range specified
Options:
-
in_range
- the range of allowed values for this attribute -
with_low_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Defaults to the translation for :inclusion. -
with_high_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Defaults to the translation for :inclusion.
Example:
it { should ensure_inclusion_of(:age).in_range(0..100) }
19 20 21 |
# File 'lib/shoulda/active_model/matchers/ensure_inclusion_of_matcher.rb', line 19 def ensure_inclusion_of(attr) EnsureInclusionOfMatcher.new(attr) end |
#ensure_length_of(attr) ⇒ Object
Ensures that the length of the attribute is validated.
Options:
-
is_at_least
- minimum length of this attribute -
is_at_most
- maximum length of this attribute -
is_equal_to
- exact requred length of this attribute -
with_short_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Defaults to the translation for :too_short. -
with_long_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Defaults to the translation for :too_long. -
with_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Defaults to the translation for :wrong_length. Used in conjunction withis_equal_to
.
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).
(/not long enough/) }
it { should ensure_length_of(:ssn).
is_equal_to(9).
(/is invalid/) }
32 33 34 |
# File 'lib/shoulda/active_model/matchers/ensure_length_of_matcher.rb', line 32 def ensure_length_of(attr) EnsureLengthOfMatcher.new(attr) end |
#validate_acceptance_of(attr) ⇒ Object
Ensures that the model cannot be saved the given attribute is not accepted.
Options:
-
with_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Defaults to the translation for:accepted
.
Example:
it { should validate_acceptance_of(:eula) }
16 17 18 |
# File 'lib/shoulda/active_model/matchers/validate_acceptance_of_matcher.rb', line 16 def validate_acceptance_of(attr) ValidateAcceptanceOfMatcher.new(attr) end |
#validate_format_of(attr) ⇒ Object
Ensures that the model is not valid if the given attribute is not formatted correctly.
Options:
-
with_message
- value the test expects to find inerrors.on(:attribute)
.Regexp
orString
. Defaults to the translation for:blank
. -
with(string to test against)
-
not_with(string to test against)
Examples:
it { should validate_format_of(:name).
with('12345').
(/is not optional/) }
it { should validate_format_of(:name).
not_with('12D45').
(/is not optional/) }
23 24 25 |
# File 'lib/shoulda/active_model/matchers/validate_format_of_matcher.rb', line 23 def validate_format_of(attr) ValidateFormatOfMatcher.new(attr) end |
#validate_numericality_of(attr) ⇒ Object
Ensure that the attribute is numeric
Options:
-
with_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Defaults to the translation for:not_a_number
.
Example:
it { should validate_numericality_of(:age) }
15 16 17 |
# File 'lib/shoulda/active_model/matchers/validate_numericality_of_matcher.rb', line 15 def validate_numericality_of(attr) ValidateNumericalityOfMatcher.new(attr) end |
#validate_presence_of(attr) ⇒ Object
Ensures that the model is not valid if the given attribute is not present.
Options:
-
with_message
- value the test expects to find inerrors.on(:attribute)
.Regexp
orString
. Defaults to the translation for:blank
.
Examples:
it { should validate_presence_of(:name) }
it { should validate_presence_of(:name).
(/is not optional/) }
18 19 20 |
# File 'lib/shoulda/active_model/matchers/validate_presence_of_matcher.rb', line 18 def validate_presence_of(attr) ValidatePresenceOfMatcher.new(attr) end |
#validate_uniqueness_of(attr) ⇒ Object
Ensures that the model is invalid if the given attribute is not unique.
Internally, this uses values from existing models to test validations, so this will always fail if you have not saved at least one model for the model being tested, like so:
describe User do
before(:each) { User.create!(:email => '[email protected]') }
it { should validate_uniqueness_of(:email) }
end
Options:
-
with_message
- value the test expects to find inerrors.on(:attribute)
.Regexp
orString
. Defaults to the translation for:taken
. -
scoped_to
- field(s) to scope the uniqueness to. -
case_insensitive
- ensures that the validation does not check case. Off by default. Ignored by non-text attributes.
Examples:
it { should validate_uniqueness_of(:keyword) }
it { should validate_uniqueness_of(:keyword).(/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 }
33 34 35 |
# File 'lib/shoulda/active_model/matchers/validate_uniqueness_of_matcher.rb', line 33 def validate_uniqueness_of(attr) ValidateUniquenessOfMatcher.new(attr) end |