module MiniTest::Matchers::ActiveModel
Public Instance Methods
Ensures that the model is invalid if the given attribute is not accepted.
Options:
-
accept_with
- value that is considered accepted.it { must
validate_acceptance_of
(:eula) } it { mustvalidate_acceptance_of
(:terms_of_service).accept_with(true) }
# File lib/matchers/validate_acceptance_matcher.rb, line 11 def validate_acceptance_of attr ValidateAcceptanceMatcher.new attr end
Ensures that the model is invalid if the given association name is not valid itself.
it { must validate_associated(:parent) } it { must validate_associated(:children) }
# File lib/matchers/validate_associated_matcher.rb, line 9 def validate_associated association_name ValidateAssociated.new association_name end
Ensures that the model’s attribute matches confirmation.
it { must validate_confirmation_of :password }
# File lib/matchers/validate_confirmation_matcher.rb, line 7 def validate_confirmation_of attr ValidationMatcher.new attr, :confirmation end
TODO: Add documentation
# File lib/matchers/validate_exclusion_matcher.rb, line 5 def validate_exclusion_of attr ValidateExclusionMatcher.new attr end
Ensures that the model is invalid if the given attribute is not formatted correctly.
Options:
-
to_allow
- string to test against that it is valid. -
to_not_allow
- string to test against that it is not valid.it { must
validate_format_of
(:email).to_allow(‘foo@bar.com’) } it { mustvalidate_format_of
(:email).to_not_allow(‘foo_bar_com’) }
# File lib/matchers/validate_format_matcher.rb, line 13 def validate_format_of attr ValidateFormatMatcher.new attr end
TODO: Add documentation.
# File lib/matchers/validate_inclusion_matcher.rb, line 5 def validate_inclusion_of attr ValidateInclusionMatcher.new attr end
Ensures that the length/size of the attribute is validated. You must supply at least one of the following options:
Options:
-
with_minimum
- ensures the minimum length of the attribute. Aliased as:with_min
andis_at_least
. -
with_maximum
- ensures the maximum length of the attribute. Aliased as:with_max
andis_at_most
. -
within
- a range specifying the minimum and maximum length of the attribute. Aliased as:in
. -
is
- ensures the exact length of the attribute. Aliased as:is_equal_to
.it { must
validate_length_of
(:name).with_minimum(10) } it { mustvalidate_length_of
(:name).with_min(10) } it { mustvalidate_length_of
(:name).is_at_least(10) } it { mustvalidate_length_of
(:name).with_maximum(100) } it { mustvalidate_length_of
(:name).with_max(100) } it { mustvalidate_length_of
(:name).is_at_most(100) } it { mustvalidate_length_of
(:name).within(10..100) } it { mustvalidate_length_of
(:name).in(10..100) } it { mustvalidate_length_of
(:password).is(8) } it { mustvalidate_length_of
(:password).is_equal_to(8) }
This matcher is also aliased as:
-
validate_size_of
. -
ensure_length_of
. -
ensure_size_of
.
So you can do something like:
it { must ensure_length_of(:name).is_at_least(10) } it { must ensure_size_of(:name).is_at_most(100) } it { must validate_size_of(:name).in(10..100) }
# File lib/matchers/validate_length_matcher.rb, line 39 def validate_length_of attr ValidateLengthMatcher.new attr end
Ensures that the model is invalid if the given attribute is not present.
it { must validate_presence_of(:name) }
# File lib/matchers/validate_presence_matcher.rb, line 7 def validate_presence_of attr ValidationMatcher.new attr, :presence end
Ensures that the model is invalid if the given attribute is not unique.
Options:
-
scoped_to
- field(s) to scope the uniqueness to. -
case_insensitive
- ensures that the validation doesn’t check case. Off by default. Ignored by non-text attributes.it { must
validate_uniqueness_of
(:email) } it { mustvalidate_uniqueness_of
(:email).scoped_to(:name) } it { mustvalidate_uniqueness_of
(:email).scoped_to(:first_name, :last_name) } it { mustvalidate_uniqueness_of
(:keyword).case_insensitive }
# File lib/matchers/validate_uniqueness_matcher.rb, line 15 def validate_uniqueness_of attr ValidateUniquenessMatcher.new attr end