thoughtbot--shoulda-matchers/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher...

75 lines
2.3 KiB
Ruby
Raw Normal View History

2011-10-16 17:49:58 +00:00
require 'spec_helper'
describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
2012-12-20 05:04:27 +00:00
context 'an attribute which must be excluded from a range' do
it 'accepts ensuring the correct range' do
validating_exclusion(:in => 2..5).
should ensure_exclusion_of(:attr).in_range(2..5)
2011-10-16 17:49:58 +00:00
end
2012-12-20 05:04:27 +00:00
it 'rejects ensuring excluded value' do
validating_exclusion(:in => 2..5).
should_not ensure_exclusion_of(:attr).in_range(2..6)
2011-10-16 17:49:58 +00:00
end
2012-12-20 05:04:27 +00:00
it 'does not override the default message with a blank' do
validating_exclusion(:in => 2..5).
should ensure_exclusion_of(:attr).in_range(2..5).with_message(nil)
2011-10-16 17:49:58 +00:00
end
end
2012-12-20 05:04:27 +00:00
context 'an attribute with a custom validation message' do
it 'accepts ensuring the correct range' do
validating_exclusion(:in => 2..4, :message => 'not good').
should ensure_exclusion_of(:attr).in_range(2..4).with_message(/not good/)
2011-10-16 17:49:58 +00:00
end
end
2012-12-20 05:04:27 +00:00
context 'an attribute with custom range validations' do
it 'accepts ensuring the correct range and messages' do
model = custom_validation do
if attr >= 2 && attr <= 5
errors.add(:attr, 'should be out of this range')
2011-10-16 17:49:58 +00:00
end
end
2012-12-20 05:04:27 +00:00
model.should ensure_exclusion_of(:attr).in_range(2..5).
with_message(/should be out of this range/)
2011-10-16 17:49:58 +00:00
end
end
2012-12-20 05:04:27 +00:00
context 'an attribute which must be excluded from an array' do
it 'accepts with correct array' do
validating_exclusion(:in => %w(one two)).
should ensure_exclusion_of(:attr).in_array(%w(one two))
2012-08-19 09:07:05 +00:00
end
2012-12-20 05:04:27 +00:00
it 'rejects when only part of array matches' do
validating_exclusion(:in => %w(one two)).
should_not ensure_exclusion_of(:attr).in_array(%w(one wrong_value))
2012-08-19 09:07:05 +00:00
end
2012-12-20 05:04:27 +00:00
it 'rejects when array does not match at all' do
validating_exclusion(:in => %w(one two)).
should_not ensure_exclusion_of(:attr).in_array(%w(cat dog))
2012-08-19 09:07:05 +00:00
end
2012-12-20 05:04:27 +00:00
it 'has correct description' do
ensure_exclusion_of(:attr).in_array([true, 'dog']).description.
should == 'ensure exclusion of attr in [true, "dog"]'
end
def validating_exclusion(options)
define_model(:example, :attr => :string) do
validates_exclusion_of :attr, options
end.new
2012-08-19 09:07:05 +00:00
end
2012-12-20 05:04:27 +00:00
end
2012-08-19 09:07:05 +00:00
2012-12-20 05:04:27 +00:00
def validating_exclusion(options)
define_model(:example, :attr => :integer) do
validates_exclusion_of :attr, options
end.new
2012-08-19 09:07:05 +00:00
end
2011-10-16 17:49:58 +00:00
end