module Minitest::Rails::Assertions::Validations

Constants

OPTIONS

Public Instance Methods

assert_associated(subject, attribute, opts = true) click to toggle source
# File lib/minitest/rails/assertions/validations.rb, line 22
def assert_associated(subject, attribute, opts = true)
  assert_validates subject, attribute, associated: opts
end
assert_validates(subject, attribute, types) click to toggle source
# File lib/minitest/rails/assertions/validations.rb, line 32
def assert_validates(subject, attribute, types)
  types.each do |type, opts|
    validators = assert_validator(type, subject, attribute)
    options = OPTIONS[type.to_sym].merge(opts.is_a?(Hash) ? opts : {})
    assert_equal options, validator_options(validators)
  end
end
assert_validator(type, subject, attribute) click to toggle source
# File lib/minitest/rails/assertions/validations.rb, line 40
def assert_validator(type, subject, attribute)
  assert_respond_to subject, :validators_on
  validators = validator_of(type.to_sym, subject, attribute)
  refute validators.empty?, "Expected #{subject} has #{type} validator"
  validators
end

Private Instance Methods

validator_of(type, subject, attribute) click to toggle source
# File lib/minitest/rails/assertions/validations.rb, line 49
def validator_of(type, subject, attribute)
  Array(subject.validators_on(attribute).select { |v| v.kind == type })
end
validator_options(validators) click to toggle source
# File lib/minitest/rails/assertions/validations.rb, line 53
def validator_options(validators)
  validator_options = validators.each_with_object({}) do |v, hash|
    hash.merge!(v.options)
  end
end