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

147 lines
4.4 KiB
Ruby
Raw Normal View History

require 'spec_helper'
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
2014-01-17 22:01:43 +00:00
expect(matcher.description).to eq 'only allow numeric values for attr'
end
2012-12-20 05:04:27 +00:00
end
2012-12-20 05:04:27 +00:00
context 'with a model with a numericality validation' do
it 'accepts' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality).to matcher
end
2012-12-20 05:04:27 +00:00
it 'does not override the default message with a blank' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality).to matcher.with_message(nil)
end
end
2012-12-20 05:04:27 +00:00
context 'with a model without a numericality validation' do
it 'rejects' do
2014-01-17 22:01:43 +00:00
expect(define_model(:example, attr: :string).new).not_to matcher
end
it 'rejects with the ActiveRecord :not_a_number message' do
the_matcher = matcher
2014-01-17 20:20:44 +00:00
the_matcher.matches?(define_model(:example, attr: :string).new)
2014-01-17 22:01:43 +00:00
expect(the_matcher.failure_message_when_negated).to include 'Did not expect errors to include "is not a number"'
end
2012-12-20 05:04:27 +00:00
it 'rejects with the ActiveRecord :not_an_integer message' do
the_matcher = matcher.only_integer
2014-01-17 20:20:44 +00:00
the_matcher.matches?(define_model(:example, attr: :string).new)
2014-01-17 22:01:43 +00:00
expect(the_matcher.failure_message).to include 'Expected errors to include "must be an integer"'
end
it 'rejects with the ActiveRecord :odd message' do
the_matcher = matcher.odd
2014-01-17 20:20:44 +00:00
the_matcher.matches?(define_model(:example, attr: :string).new)
2014-01-17 22:01:43 +00:00
expect(the_matcher.failure_message).to include 'Expected errors to include "must be odd"'
end
it 'rejects with the ActiveRecord :even message' do
the_matcher = matcher.even
2014-01-17 20:20:44 +00:00
the_matcher.matches?(define_model(:example, attr: :string).new)
2014-01-17 22:01:43 +00:00
expect(the_matcher.failure_message).to include 'Expected errors to include "must be even"'
end
end
2012-12-20 05:04:27 +00:00
context 'with the only_integer option' do
it 'allows integer values for that attribute' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality(only_integer: true)).to matcher.only_integer
end
2012-12-20 05:04:27 +00:00
it 'rejects when the model does not enforce integer values' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality).not_to matcher.only_integer
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)
2014-01-17 22:01:43 +00:00
expect(the_matcher.failure_message).to include 'Expected errors to include "must be an integer"'
end
end
context 'with the odd option' do
it 'allows odd number values for that attribute' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality(odd: true)).to matcher.odd
end
it 'rejects when the model does not enforce odd number values' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality).not_to matcher.odd
end
it 'rejects with the ActiveRecord :odd message' do
the_matcher = matcher.odd
the_matcher.matches?(validating_numericality)
2014-01-17 22:01:43 +00:00
expect(the_matcher.failure_message).to include 'Expected errors to include "must be odd"'
end
end
context 'with the even option' do
it 'allows even number values for that attribute' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality(even: true)).to matcher.even
end
it 'rejects when the model does not enforce even number values' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality).not_to matcher.even
end
it 'rejects with the ActiveRecord :even message' do
the_matcher = matcher.even
the_matcher.matches?(validating_numericality)
2014-01-17 22:01:43 +00:00
expect(the_matcher.failure_message).to include 'Expected errors to include "must be even"'
end
end
2012-12-20 05:04:27 +00:00
context 'with a custom validation message' do
it 'accepts when the messages match' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality(message: 'custom')).
to matcher.with_message(/custom/)
end
2012-12-20 05:04:27 +00:00
it 'rejects when the messages do not match' do
2014-01-17 22:01:43 +00:00
expect(validating_numericality(message: 'custom')).
not_to matcher.with_message(/wrong/)
end
2012-12-20 05:04:27 +00:00
end
context 'when the subject is stubbed' do
it 'retains stubs on submatchers' do
2014-01-17 20:20:44 +00:00
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!)
2014-01-17 22:01:43 +00:00
expect(subject).to matcher.odd
end
end
2012-12-20 05:04:27 +00:00
def validating_numericality(options = {})
2014-01-17 20:20:44 +00:00
define_model :example, attr: :string do
2012-12-20 05:04:27 +00:00
validates_numericality_of :attr, options
end.new
end
2012-12-20 05:04:27 +00:00
def matcher
validate_numericality_of(:attr)
end
end