ensure_exclusion_of => validate_exclusion_of

This commit is contained in:
Maxim Kaschenko 2014-05-10 15:59:21 +07:00 committed by Elliot Winkler
parent fcdd86021d
commit acb02d0448
4 changed files with 36 additions and 27 deletions

View File

@ -14,8 +14,8 @@ complex, and error-prone.
the `validates_format_of` validation.
* **[validate_inclusion_of](lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb)**
tests usage of `validates_inclusion_of`.
* **[ensure_exclusion_of](lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb)** tests
usage of `validates_exclusion_of`.
* **[validate_exclusion_of](lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb)**
tests usage of `validates_exclusion_of`.
* **[ensure_length_of](lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb)** tests usage
of `validates_length_of`.
* **[have_secure_password](lib/shoulda/matchers/active_model/have_secure_password_matcher.rb)** tests

View File

@ -6,7 +6,7 @@ require 'shoulda/matchers/active_model/allow_value_matcher'
require 'shoulda/matchers/active_model/disallow_value_matcher'
require 'shoulda/matchers/active_model/ensure_length_of_matcher'
require 'shoulda/matchers/active_model/validate_inclusion_of_matcher'
require 'shoulda/matchers/active_model/ensure_exclusion_of_matcher'
require 'shoulda/matchers/active_model/validate_exclusion_of_matcher'
require 'shoulda/matchers/active_model/validate_absence_of_matcher'
require 'shoulda/matchers/active_model/validate_presence_of_matcher'
require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher'

View File

@ -1,7 +1,7 @@
module Shoulda
module Matchers
module ActiveModel
# The `ensure_exclusion_of` matcher tests usage of the
# The `validate_exclusion_of` matcher tests usage of the
# `validates_exclusion_of` validation, asserting that an attribute cannot
# take a blacklist of values, and inversely, can take values outside of
# this list.
@ -18,14 +18,14 @@ module Shoulda
# # RSpec
# describe Game do
# it do
# should ensure_exclusion_of(:supported_os).
# should validate_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux'])
# end
# end
#
# # Test::Unit
# class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:supported_os).
# should validate_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux'])
# end
#
@ -41,14 +41,14 @@ module Shoulda
# # RSpec
# describe Game do
# it do
# should ensure_exclusion_of(:floors_with_enemies).
# should validate_exclusion_of(:floors_with_enemies).
# in_range(5..8)
# end
# end
#
# # Test::Unit
# class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:floors_with_enemies).
# should validate_exclusion_of(:floors_with_enemies).
# in_range(5..8)
# end
#
@ -70,7 +70,7 @@ module Shoulda
# # RSpec
# describe Game do
# it do
# should ensure_exclusion_of(:weapon).
# should validate_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon')
# end
@ -78,19 +78,20 @@ module Shoulda
#
# # Test::Unit
# class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:weapon).
# should validate_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon')
# end
#
# @return [EnsureExclusionOfMatcher]
# @return [ValidateExclusionOfMatcher]
#
def ensure_exclusion_of(attr)
EnsureExclusionOfMatcher.new(attr)
def validate_exclusion_of(attr)
ValidateExclusionOfMatcher.new(attr)
end
alias_method :ensure_exclusion_of, :validate_exclusion_of
# @private
class EnsureExclusionOfMatcher < ValidationMatcher
class ValidateExclusionOfMatcher < ValidationMatcher
def initialize(attribute)
super(attribute)
@array = nil

View File

@ -1,39 +1,47 @@
require 'spec_helper'
describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
describe Shoulda::Matchers::ActiveModel do
describe '#ensure_exclusion_of' do
it 'is aliased to #validate_exclusion_of' do
expect(method(:ensure_exclusion_of)).to eq(method(:validate_exclusion_of))
end
end
end
describe Shoulda::Matchers::ActiveModel::ValidateExclusionOfMatcher do
context 'an attribute which must be excluded from a range' do
it 'accepts ensuring the correct range' do
expect(validating_exclusion(in: 2..5)).
to ensure_exclusion_of(:attr).in_range(2..5)
to validate_exclusion_of(:attr).in_range(2..5)
end
it 'rejects ensuring excluded value' do
expect(validating_exclusion(in: 2..5)).
not_to ensure_exclusion_of(:attr).in_range(2..6)
not_to validate_exclusion_of(:attr).in_range(2..6)
end
it 'does not override the default message with a blank' do
expect(validating_exclusion(in: 2..5)).
to ensure_exclusion_of(:attr).in_range(2..5).with_message(nil)
to validate_exclusion_of(:attr).in_range(2..5).with_message(nil)
end
end
context 'an attribute which must be excluded from a range with excluded end' do
it 'accepts ensuring the correct range' do
expect(validating_exclusion(in: 2...5)).
to ensure_exclusion_of(:attr).in_range(2...5)
to validate_exclusion_of(:attr).in_range(2...5)
end
it 'rejects ensuring excluded value' do
expect(validating_exclusion(in: 2...5)).
not_to ensure_exclusion_of(:attr).in_range(2...4)
not_to validate_exclusion_of(:attr).in_range(2...4)
end
end
context 'an attribute with a custom validation message' do
it 'accepts ensuring the correct range' do
expect(validating_exclusion(in: 2..4, message: 'not good')).
to ensure_exclusion_of(:attr).in_range(2..4).with_message(/not good/)
to validate_exclusion_of(:attr).in_range(2..4).with_message(/not good/)
end
end
@ -45,7 +53,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
end
end
expect(model).to ensure_exclusion_of(:attr).in_range(2..5).
expect(model).to validate_exclusion_of(:attr).in_range(2..5).
with_message(/should be out of this range/)
model = custom_validation do
@ -54,7 +62,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
end
end
expect(model).to ensure_exclusion_of(:attr).in_range(2...5).
expect(model).to validate_exclusion_of(:attr).in_range(2...5).
with_message(/should be out of this range/)
end
end
@ -62,21 +70,21 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
context 'an attribute which must be excluded from an array' do
it 'accepts with correct array' do
expect(validating_exclusion(in: %w(one two))).
to ensure_exclusion_of(:attr).in_array(%w(one two))
to validate_exclusion_of(:attr).in_array(%w(one two))
end
it 'rejects when only part of array matches' do
expect(validating_exclusion(in: %w(one two))).
not_to ensure_exclusion_of(:attr).in_array(%w(one wrong_value))
not_to validate_exclusion_of(:attr).in_array(%w(one wrong_value))
end
it 'rejects when array does not match at all' do
expect(validating_exclusion(in: %w(one two))).
not_to ensure_exclusion_of(:attr).in_array(%w(cat dog))
not_to validate_exclusion_of(:attr).in_array(%w(cat dog))
end
it 'has correct description' do
expect(ensure_exclusion_of(:attr).in_array([true, 'dog']).description).
expect(validate_exclusion_of(:attr).in_array([true, 'dog']).description).
to eq 'ensure exclusion of attr in [true, "dog"]'
end