diff --git a/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb index 8d2f85ea..9d3d0f78 100644 --- a/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb @@ -432,7 +432,7 @@ module Shoulda end def given_numeric_column? - [:integer, :float].include?(column_type) + [:integer, :float, :decimal].include?(column_type) end private diff --git a/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb index 3ef09039..31d2d8af 100644 --- a/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb @@ -150,6 +150,18 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m to raise_error(described_class::IneffectiveTestError) end end + + context 'when the column is a decimal column' do + it 'raises an IneffectiveTestError' do + record = build_record_validating_numericality( + column_type: :decimal, + ) + assertion = -> { expect(record).to validate_numericality } + + expect(&assertion). + to raise_error(described_class::IneffectiveTestError) + end + end end context 'and not validating anything' do @@ -233,6 +245,17 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m expect(record).to validate_numericality.odd end end + + context 'when the column is a decimal column' do + it 'accepts (and does not raise an error)' do + record = build_record_validating_numericality( + column_type: :decimal, + odd: true, + ) + + expect(record).to validate_numericality.odd + end + end end context 'and not validating with odd' do @@ -276,6 +299,17 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m expect(record).to validate_numericality.even end end + + context 'when the column is a decimal column' do + it 'accepts (and does not raise an error)' do + record = build_record_validating_numericality( + column_type: :decimal, + even: true, + ) + + expect(record).to validate_numericality.even + end + end end context 'and not validating with even' do @@ -319,6 +353,17 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m expect(record).to validate_numericality.is_less_than_or_equal_to(18) end end + + context 'when the column is a decimal column' do + it 'accepts (and does not raise an error)' do + record = build_record_validating_numericality( + column_type: :decimal, + less_than_or_equal_to: 18, + ) + + expect(record).to validate_numericality.is_less_than_or_equal_to(18) + end + end end context 'and not validating with less_than_or_equal_to' do @@ -366,6 +411,17 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m expect(record).to validate_numericality.is_less_than(18) end end + + context 'when the column is a decimal column' do + it 'accepts (and does not raise an error)' do + record = build_record_validating_numericality( + column_type: :decimal, + less_than: 18, + ) + + expect(record).to validate_numericality.is_less_than(18) + end + end end context 'and not validating with less_than' do @@ -411,6 +467,17 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m expect(record).to validate_numericality.is_equal_to(18) end end + + context 'when the column is a decimal column' do + it 'accepts (and does not raise an error)' do + record = build_record_validating_numericality( + column_type: :decimal, + equal_to: 18, + ) + + expect(record).to validate_numericality.is_equal_to(18) + end + end end context 'and not validating with equal_to' do @@ -462,6 +529,19 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m is_greater_than_or_equal_to(18) end end + + context 'when the column is a decimal column' do + it 'accepts (and does not raise an error)' do + record = build_record_validating_numericality( + column_type: :decimal, + greater_than_or_equal_to: 18, + ) + + expect(record). + to validate_numericality. + is_greater_than_or_equal_to(18) + end + end end context 'not validating with greater_than_or_equal_to' do @@ -513,6 +593,19 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m is_greater_than(18) end end + + context 'when the column is a decimal column' do + it 'accepts (and does not raise an error)' do + record = build_record_validating_numericality( + column_type: :decimal, + greater_than: 18, + ) + + expect(record). + to validate_numericality. + is_greater_than(18) + end + end end context 'and not validating with greater_than' do