Module: Shoulda::Matchers::ActiveModel

Defined in:
lib/shoulda/matchers/active_model.rb,
lib/shoulda/matchers/active_model/errors.rb,
lib/shoulda/matchers/active_model/helpers.rb,
lib/shoulda/matchers/active_model/validation_matcher.rb,
lib/shoulda/matchers/active_model/allow_value_matcher.rb,
lib/shoulda/matchers/active_model/disallow_value_matcher.rb,
lib/shoulda/matchers/active_model/exception_message_finder.rb,
lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb,
lib/shoulda/matchers/active_model/validation_message_finder.rb,
lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb,
lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb,
lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb,
lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb,
lib/shoulda/matchers/active_model/have_secure_password_matcher.rb,
lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb,
lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb,
lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb,
lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb,
lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb,
lib/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher.rb,
lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb,
lib/shoulda/matchers/active_model/numericality_matchers/even_number_matcher.rb,
lib/shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher.rb,
lib/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Helpers, NumericalityMatchers Classes: AllowMassAssignmentOfMatcher, AllowValueMatcher, CouldNotDetermineValueOutsideOfArray, CouldNotSetPasswordError, DisallowValueMatcher, EnsureExclusionOfMatcher, EnsureInclusionOfMatcher, EnsureLengthOfMatcher, ExceptionMessageFinder, HaveSecurePasswordMatcher, NonNullableBooleanError, ValidateAbsenceOfMatcher, ValidateAcceptanceOfMatcher, ValidateConfirmationOfMatcher, ValidateNumericalityOfMatcher, ValidatePresenceOfMatcher, ValidateUniquenessOfMatcher, ValidationMatcher, ValidationMessageFinder

Instance Method Summary (collapse)

Instance Method Details

- (Object) allow_mass_assignment_of(value)

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

In Rails 3.1 you can check role as well:

it { should allow_mass_assignment_of(:first_name).as(:admin) }


14
15
16
# File 'lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb', line 14

def allow_mass_assignment_of(value)
  AllowMassAssignmentOfMatcher.new(value)
end

- (Object) allow_value(*values)

Ensures that the attribute can be set to the given value or values. If multiple values are given the match succeeds only if all given values are allowed. Otherwise, the matcher fails at the first bad value in the argument list (the remaining arguments are not processed then).

Options:

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or string. If omitted, the test looks for any errors in errors.on(:attribute).

  • strict - expects the model to raise an exception when the validation fails rather than adding to the errors collection. Used for testing `validates!` and the `strict: true` validation options.

Example:

it { should_not allow_value('bad').for(:isbn) }
it { should allow_value('isbn 1 2345 6789 0').for(:isbn) }


22
23
24
25
26
27
28
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 22

def allow_value(*values)
  if values.empty?
    raise ArgumentError, 'need at least one argument'
  else
    AllowValueMatcher.new(*values)
  end
end

- (Object) ensure_exclusion_of(attr)

Ensure that the attribute's value is not in the range specified

Options:

  • in_array - the array of not allowed values for this attribute

  • in_range - the range of not allowed values for this attribute

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :exclusion.

Example:

it { should ensure_exclusion_of(:age).in_range(30..60) }


17
18
19
# File 'lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb', line 17

def ensure_exclusion_of(attr)
  EnsureExclusionOfMatcher.new(attr)
end

- (Object) ensure_inclusion_of(attr)

Ensure that the attribute's value is in the range specified

Options:

  • in_array - the array of allowed values for this attribute

  • in_range - the range of allowed values for this attribute

  • with_low_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :inclusion.

  • with_high_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :inclusion.

Example:

it { should ensure_inclusion_of(:age).in_range(0..100) }


20
21
22
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 20

def ensure_inclusion_of(attr)
  EnsureInclusionOfMatcher.new(attr)
end

- (Object) ensure_length_of(attr)

Ensures that the length of the attribute is validated. Only works with string/text columns because it uses a string to check length.

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 in errors.on(:attribute). Regexp or string. Defaults to the translation for :too_short.

  • with_long_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :too_long.

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :wrong_length. Used in conjunction with is_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).
              with_short_message(/not long enough/) }
it { should ensure_length_of(:ssn).
              is_equal_to(9).
              with_message(/is invalid/) }


33
34
35
# File 'lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb', line 33

def ensure_length_of(attr)
  EnsureLengthOfMatcher.new(attr)
end

- (Object) have_secure_password

Ensures that the model exhibits behavior added by has_secure_password.

Example:

it { should have_secure_password }


9
10
11
# File 'lib/shoulda/matchers/active_model/have_secure_password_matcher.rb', line 9

def have_secure_password
  HaveSecurePasswordMatcher.new
end

- (Object) validate_absence_of(attr)

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

Options:

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or String. Defaults to the translation for :present.

Examples:

it { should validate_absence_of(:name) }
it { should validate_absence_of(:name).
              with_message(/may not be set/) }


16
17
18
# File 'lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb', line 16

def validate_absence_of(attr)
  ValidateAbsenceOfMatcher.new(attr)
end

- (Object) validate_acceptance_of(attr)

Ensures that the model cannot be saved if the given attribute is not accepted.

Options:

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :accepted.

Example:

it { should validate_acceptance_of(:eula) }


16
17
18
# File 'lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb', line 16

def validate_acceptance_of(attr)
  ValidateAcceptanceOfMatcher.new(attr)
end

- (Object) validate_confirmation_of(attr)

Ensures that the model's attribute matches confirmation

Example:

it { should validate_confirmation_of(:password) }


9
10
11
# File 'lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb', line 9

def validate_confirmation_of(attr)
  ValidateConfirmationOfMatcher.new(attr)
end

- (Object) validate_numericality_of(attr)

Ensure that the attribute is numeric.

Options:

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or string. Defaults to the translation for :not_a_number.

  • only_integer - allows only integer values

  • odd - Specifies the value must be an odd number.

  • even - Specifies the value must be an even number.

  • allow_nil - allows nil values

Examples:

it { should validate_numericality_of(:price) }
it { should validate_numericality_of(:age).only_integer }
it { should validate_numericality_of(:frequency).odd }
it { should validate_numericality_of(:frequency).even }
it { should validate_numericality_of(:rank).is_less_than_or_equal_to(10).allow_nil }


22
23
24
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 22

def validate_numericality_of(attr)
  ValidateNumericalityOfMatcher.new(attr)
end

- (Object) validate_presence_of(attr)

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

Options:

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or String. Defaults to the translation for :blank.

Examples:

it { should validate_presence_of(:name) }
it { should validate_presence_of(:name).
              with_message(/is not optional/) }


18
19
20
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 18

def validate_presence_of(attr)
  ValidatePresenceOfMatcher.new(attr)
end

- (Object) validate_uniqueness_of(attr)

Ensures that the model is invalid if the given attribute is not unique. It uses the first existing record or creates a new one if no record exists in the database. It simply uses `validate: false` to get around validations, so it will probably fail if there are `NOT NULL` constraints. In that case, you must create a record before calling `validate_uniqueness_of`.

Example:

it { should validate_uniqueness_of(:email) }

Options:

  • with_message - value the test expects to find in errors.on(:attribute). Regexp or String. 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).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 }


31
32
33
# File 'lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb', line 31

def validate_uniqueness_of(attr)
  ValidateUniquenessOfMatcher.new(attr)
end