2010-12-13 22:28:59 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2011-05-06 11:51:12 +00:00
|
|
|
describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
|
2012-12-20 05:04:27 +00:00
|
|
|
context '#description' do
|
|
|
|
it 'states that it allows only numeric values' do
|
2013-08-23 18:44:57 +00:00
|
|
|
matcher.description.should eq 'only allow numeric values for attr'
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
2012-12-20 05:04:27 +00:00
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
context 'with a model with a numericality validation' do
|
|
|
|
it 'accepts' do
|
|
|
|
validating_numericality.should matcher
|
2012-10-16 17:45:06 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'does not override the default message with a blank' do
|
|
|
|
validating_numericality.should matcher.with_message(nil)
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
context 'with a model without a numericality validation' do
|
|
|
|
it 'rejects' do
|
|
|
|
define_model(:example, :attr => :string).new.should_not matcher
|
2011-10-27 02:21:06 +00:00
|
|
|
end
|
|
|
|
|
2013-04-12 17:11:07 +00:00
|
|
|
it 'rejects with the ActiveRecord :not_a_number message' do
|
|
|
|
the_matcher = matcher
|
|
|
|
|
|
|
|
the_matcher.matches?(define_model(:example, :attr => :string).new)
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
the_matcher.failure_message_when_negated.should include 'Did not expect errors to include "is not a number"'
|
2013-04-12 17:11:07 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects with the ActiveRecord :not_an_integer message' do
|
|
|
|
the_matcher = matcher.only_integer
|
2012-10-16 17:45:06 +00:00
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
the_matcher.matches?(define_model(:example, :attr => :string).new)
|
2012-10-16 17:45:06 +00:00
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
the_matcher.failure_message.should include 'Expected errors to include "must be an integer"'
|
2011-10-27 02:21:06 +00:00
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
|
|
|
it 'rejects with the ActiveRecord :odd message' do
|
|
|
|
the_matcher = matcher.odd
|
|
|
|
|
|
|
|
the_matcher.matches?(define_model(:example, :attr => :string).new)
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
the_matcher.failure_message.should include 'Expected errors to include "must be odd"'
|
2013-03-19 04:50:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects with the ActiveRecord :even message' do
|
|
|
|
the_matcher = matcher.even
|
|
|
|
|
|
|
|
the_matcher.matches?(define_model(:example, :attr => :string).new)
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
the_matcher.failure_message.should include 'Expected errors to include "must be even"'
|
2013-03-19 04:50:39 +00:00
|
|
|
end
|
2011-10-27 02:21:06 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
context 'with the only_integer option' do
|
|
|
|
it 'allows integer values for that attribute' do
|
|
|
|
validating_numericality(:only_integer => true).should matcher.only_integer
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects when the model does not enforce integer values' do
|
|
|
|
validating_numericality.should_not matcher.only_integer
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects with the ActiveRecord :not_an_integer message' do
|
|
|
|
the_matcher = matcher.only_integer
|
|
|
|
|
|
|
|
the_matcher.matches?(validating_numericality)
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
the_matcher.failure_message.should include 'Expected errors to include "must be an integer"'
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-19 04:50:39 +00:00
|
|
|
context 'with the odd option' do
|
|
|
|
it 'allows odd number values for that attribute' do
|
|
|
|
validating_numericality(:odd => true).should matcher.odd
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects when the model does not enforce odd number values' do
|
|
|
|
validating_numericality.should_not matcher.odd
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects with the ActiveRecord :odd message' do
|
|
|
|
the_matcher = matcher.odd
|
|
|
|
|
|
|
|
the_matcher.matches?(validating_numericality)
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
the_matcher.failure_message.should include 'Expected errors to include "must be odd"'
|
2013-03-19 04:50:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the even option' do
|
|
|
|
it 'allows even number values for that attribute' do
|
|
|
|
validating_numericality(:even => true).should matcher.even
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects when the model does not enforce even number values' do
|
|
|
|
validating_numericality.should_not matcher.even
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects with the ActiveRecord :even message' do
|
|
|
|
the_matcher = matcher.even
|
|
|
|
|
|
|
|
the_matcher.matches?(validating_numericality)
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
the_matcher.failure_message.should include 'Expected errors to include "must be even"'
|
2013-03-19 04:50:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
context 'with a custom validation message' do
|
|
|
|
it 'accepts when the messages match' do
|
2013-03-15 06:32:53 +00:00
|
|
|
validating_numericality(:message => 'custom').
|
|
|
|
should matcher.with_message(/custom/)
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects when the messages do not match' do
|
2013-03-15 06:32:53 +00:00
|
|
|
validating_numericality(:message => 'custom').
|
|
|
|
should_not matcher.with_message(/wrong/)
|
2012-10-16 17:45:06 +00:00
|
|
|
end
|
2012-12-20 05:04:27 +00:00
|
|
|
end
|
2012-10-16 17:45:06 +00:00
|
|
|
|
2013-07-10 17:05:41 +00:00
|
|
|
context 'when the subject is stubbed' do
|
|
|
|
it 'retains stubs on submatchers' do
|
|
|
|
subject = define_model :example, :attr => :string do
|
|
|
|
validates_numericality_of :attr, :odd => true
|
|
|
|
before_validation :set_attr!
|
|
|
|
def set_attr!; self.attr = 5 end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
subject.stubs(:set_attr!)
|
|
|
|
subject.should matcher.odd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
def validating_numericality(options = {})
|
|
|
|
define_model :example, :attr => :string do
|
|
|
|
validates_numericality_of :attr, options
|
|
|
|
end.new
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
def matcher
|
|
|
|
validate_numericality_of(:attr)
|
2012-10-16 17:45:06 +00:00
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|