Module: MiniTest::Matchers::ActiveModel

Defined in:
lib/matchers/helpers.rb,
lib/matchers/validation_matcher.rb,
lib/matchers/validate_length_matcher.rb,
lib/matchers/validate_format_matcher.rb,
lib/matchers/validate_presence_matcher.rb,
lib/matchers/validate_exclusion_matcher.rb,
lib/matchers/validate_inclusion_matcher.rb,
lib/matchers/validate_uniqueness_matcher.rb,
lib/matchers/validate_acceptance_matcher.rb,
lib/matchers/validate_associated_matcher.rb,
lib/matchers/validate_confirmation_matcher.rb

Defined Under Namespace

Modules: Helpers Classes: ValidateAcceptanceMatcher, ValidateAssociated, ValidateExclusionMatcher, ValidateFormatMatcher, ValidateInclusionMatcher, ValidateLengthMatcher, ValidateUniquenessMatcher, ValidationMatcher

Instance Method Summary collapse

Instance Method Details

#validate_acceptance_of(attr) ⇒ Object

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 { must validate_acceptance_of(:terms_of_service).accept_with(true) }


11
12
13
# File 'lib/matchers/validate_acceptance_matcher.rb', line 11

def validate_acceptance_of attr
  ValidateAcceptanceMatcher.new attr
end

#validate_associated(association_name) ⇒ Object

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) }

9
10
11
# File 'lib/matchers/validate_associated_matcher.rb', line 9

def validate_associated association_name
  ValidateAssociated.new association_name
end

#validate_confirmation_of(attr) ⇒ Object

Ensures that the model's attribute matches confirmation.

it { must validate_confirmation_of :password }

7
8
9
# File 'lib/matchers/validate_confirmation_matcher.rb', line 7

def validate_confirmation_of attr
  ValidationMatcher.new attr, :confirmation
end

#validate_exclusion_of(attr) ⇒ Object

TODO: Add documentation


5
6
7
# File 'lib/matchers/validate_exclusion_matcher.rb', line 5

def validate_exclusion_of attr
  ValidateExclusionMatcher.new attr
end

#validate_format_of(attr) ⇒ Object

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('[email protected]') } it { must validate_format_of(:email).to_not_allow('foo_bar_com') }


13
14
15
# File 'lib/matchers/validate_format_matcher.rb', line 13

def validate_format_of attr
  ValidateFormatMatcher.new attr
end

#validate_inclusion_of(attr) ⇒ Object

TODO: Add documentation.


5
6
7
# File 'lib/matchers/validate_inclusion_matcher.rb', line 5

def validate_inclusion_of attr
  ValidateInclusionMatcher.new attr
end

#validate_length_of(attr) ⇒ Object Also known as: validate_size_of, ensure_length_of, ensure_size_of

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 and is_at_least.

  • with_maximum - ensures the maximum length of the attribute. Aliased as: with_max and is_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 { must validate_length_of(:name).with_min(10) } it { must validate_length_of(:name).is_at_least(10) } it { must validate_length_of(:name).with_maximum(100) } it { must validate_length_of(:name).with_max(100) } it { must validate_length_of(:name).is_at_most(100) } it { must validate_length_of(:name).within(10..100) } it { must validate_length_of(:name).in(10..100) } it { must validate_length_of(:password).is(8) } it { must validate_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) }

39
40
41
# File 'lib/matchers/validate_length_matcher.rb', line 39

def validate_length_of attr
  ValidateLengthMatcher.new attr
end

#validate_presence_of(attr) ⇒ Object

Ensures that the model is invalid if the given attribute is not present.

it { must validate_presence_of(:name) }

7
8
9
# File 'lib/matchers/validate_presence_matcher.rb', line 7

def validate_presence_of attr
  ValidationMatcher.new attr, :presence
end

#validate_uniqueness_of(attr) ⇒ Object

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 { must validate_uniqueness_of(:email).scoped_to(:name) } it { must validate_uniqueness_of(:email).scoped_to(:first_name, :last_name) } it { must validate_uniqueness_of(:keyword).case_insensitive }


15
16
17
# File 'lib/matchers/validate_uniqueness_matcher.rb', line 15

def validate_uniqueness_of attr
  ValidateUniquenessMatcher.new attr
end