1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Remove tests for numericality submatchers

All of these are tested indirectly through the tests for the
numericality matcher itself. There are already plenty of tests to
maintain, and so there's really no need to unit test these (not that
they were truly "unit tested" to begin with).
This commit is contained in:
Elliot Winkler 2015-12-25 17:14:05 -05:00
parent d902b72d15
commit b25f1aab28
4 changed files with 0 additions and 566 deletions

View file

@ -1,287 +0,0 @@
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::ComparisonMatcher do
it_behaves_like 'a numerical submatcher' do
subject { build_matcher }
end
shared_examples_for 'strict qualifier' do
context 'asserting strict validation when validating strictly' do
it 'accepts' do
record = instance_with_validations(
validation_qualifier => 1,
strict: true
)
matcher = build_matcher(operator: operator, value: 1).strict
expect(record).to matcher
end
end
context 'asserting non-strict validation when validating strictly' do
it 'rejects' do
record = instance_with_validations(
validation_qualifier => 1,
strict: true
)
matcher = build_matcher(operator: operator, value: 1)
expect(record).not_to matcher
end
end
context 'asserting strict validation when not validating strictly' do
it 'rejects' do
record = instance_with_validations(validation_qualifier => 1)
matcher = build_matcher(operator: operator, value: 1).strict
expect(record).not_to matcher
end
end
end
context 'when initialized without correct numerical matcher' do
it 'raises an ArgumentError' do
numericality_matcher = double
expect { described_class.new(numericality_matcher, 0, :>) }.
to raise_error(ArgumentError)
end
end
describe 'is_greater_than' do
include_examples 'strict qualifier'
it do
record = instance_with_validations(greater_than: 1.5)
matcher = build_matcher(operator: :>, value: 2)
expect(record).not_to matcher
end
it do
record = instance_with_validations(greater_than: 2)
matcher = build_matcher(operator: :>, value: 2)
expect(record).to matcher
end
it do
record = instance_with_validations(greater_than: 2.5)
matcher = build_matcher(operator: :>, value: 2)
expect(record).not_to matcher
end
it do
record = instance_without_validations
matcher = build_matcher(operator: :>, value: 2)
expect(record).not_to matcher
end
def operator
:>
end
def validation_qualifier
:greater_than
end
end
describe 'is_greater_than_or_equal_to' do
include_examples 'strict qualifier'
it do
record = instance_with_validations(greater_than_or_equal_to: 1.5)
matcher = build_matcher(operator: :>=, value: 2)
expect(record).not_to matcher
end
it do
record = instance_with_validations(greater_than_or_equal_to: 2)
matcher = build_matcher(operator: :>=, value: 2)
expect(record).to matcher
end
it do
record = instance_with_validations(greater_than_or_equal_to: 2.5)
matcher = build_matcher(operator: :>=, value: 2)
expect(record).not_to matcher
end
it do
record = instance_without_validations
matcher = build_matcher(operator: :>=, value: 2)
expect(record).not_to matcher
end
def operator
:>=
end
def validation_qualifier
:greater_than_or_equal_to
end
end
describe 'is_less_than' do
include_examples 'strict qualifier'
it do
record = instance_with_validations(less_than: 1.5)
matcher = build_matcher(operator: :<, value: 2)
expect(record).not_to matcher
end
it do
record = instance_with_validations(less_than: 2)
matcher = build_matcher(operator: :<, value: 2)
expect(record).to matcher
end
it do
record = instance_with_validations(less_than: 2.5)
matcher = build_matcher(operator: :<, value: 2)
expect(record).not_to matcher
end
it do
record = instance_without_validations
matcher = build_matcher(operator: :<, value: 2)
expect(record).not_to matcher
end
def operator
:<
end
def validation_qualifier
:less_than
end
end
describe 'is_less_than_or_equal_to' do
include_examples 'strict qualifier'
it do
record = instance_with_validations(less_than_or_equal_to: 1.5)
matcher = build_matcher(operator: :<=, value: 2)
expect(record).not_to matcher
end
it do
record = instance_with_validations(less_than_or_equal_to: 2)
matcher = build_matcher(operator: :<=, value: 2)
expect(record).to matcher
end
it do
record = instance_with_validations(less_than_or_equal_to: 2.5)
matcher = build_matcher(operator: :<=, value: 2)
expect(record).not_to matcher
end
it do
record = instance_without_validations
matcher = build_matcher(operator: :<=, value: 2)
expect(record).not_to matcher
end
def operator
:<=
end
def validation_qualifier
:less_than_or_equal_to
end
end
describe 'is_equal_to' do
include_examples 'strict qualifier'
it do
record = instance_with_validations(equal_to: 1.5)
matcher = build_matcher(operator: :==, value: 2)
expect(record).not_to matcher
end
it do
record = instance_with_validations(equal_to: 2)
matcher = build_matcher(operator: :==, value: 2)
expect(record).to matcher
end
it do
record = instance_with_validations(equal_to: 2.5)
matcher = build_matcher(operator: :==, value: 2)
expect(record).not_to matcher
end
it do
record = instance_without_validations
matcher = build_matcher(operator: :==, value: 2)
expect(record).not_to matcher
end
def operator
:==
end
def validation_qualifier
:equal_to
end
end
describe 'with_message' do
it 'verifies the message for the validation' do
instance = instance_with_validations(equal_to: 0, message: 'Must be zero')
matcher = build_matcher.with_message('Must be zero')
expect(instance).to matcher
end
end
describe '#comparison_description' do
tests = [
{ operator: :>, value: 0, expectation: 'greater than 0' },
{ operator: :>=, value: -1.0, expectation: 'greater than or equal to -1.0' },
{ operator: :==, value: 2.2, expectation: 'equal to 2.2' },
{ operator: :<, value: -3, expectation: 'less than -3' },
{ operator: :<=, value: 4, expectation: 'less than or equal to 4' },
]
tests.each do |test|
context "with :#{test[:operator]} as operator and #{test[:value]} as value" do
it do
matcher = build_matcher(operator: test[:operator], value: test[:value])
expect(matcher.comparison_description).to eq test[:expectation]
end
end
end
end
def model_with_validations(options = {})
define_model :example, attribute_name => :string do |model|
model.validates_numericality_of(attribute_name, options)
model.attr_accessible(attribute_name)
end
end
def instance_with_validations(options = {})
model_with_validations(options).new(attribute_name => '1')
end
def model_without_validations
define_model :example, attribute_name => :string do |model|
model.attr_accessible(attribute_name)
end
end
def instance_without_validations
model_without_validations.new
end
def attribute_name
:attr
end
def build_matcher(operator: :==, value: 0)
described_class.new(numericality_matcher, value, operator).for(attribute_name)
end
def numericality_matcher
double(diff_to_compare: 1, given_numeric_column?: nil)
end
end

View file

@ -1,93 +0,0 @@
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::EvenNumberMatcher do
subject { described_class.new(numericality_matcher, :attr) }
it_behaves_like 'a numerical submatcher'
context 'when the model has an even validation' do
it 'matches' do
match = subject
expect(validating_even_number).to match
end
end
context 'when the model does not have an even validation' do
it 'rejects with an appropriate failure message' do
match = subject
assertion = lambda do
expect(not_validating_even_number).to match
end
message = <<-MESSAGE.strip_heredoc
After setting :attr to "1", the matcher expected the Example to be
invalid, but it was valid instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'with custom validation message' do
it 'only accepts even number values for that attribute with that message' do
expect(validating_even_number(message: 'custom')).to subject.with_message(/custom/)
end
it 'fails even number values for that attribute with another message' do
expect(validating_even_number(message: 'custom')).not_to subject.with_message(/wrong/)
end
end
context 'asserting strict validation when validating strictly' do
it 'accepts' do
expect(validating_even_number(strict: true)).to subject.strict
end
end
context 'asserting non-strict validation when validating strictly' do
it 'rejects' do
expect(validating_even_number(strict: true)).not_to subject
end
end
context 'asserting strict validation when not validating strictly' do
it 'rejects' do
expect(validating_even_number).not_to subject.strict
end
end
context 'qualified with on and validating with on' do
it 'accepts' do
expect(validating_even_number(on: :customizable)).
to subject.on(:customizable)
end
end
context 'qualified with on but not validating with on' do
it 'accepts since the validation never considers a context' do
expect(validating_even_number).to subject.on(:customizable)
end
end
context 'not qualified with on but validating with on' do
it 'rejects since the validation never runs' do
expect(validating_even_number(on: :customizable)).
not_to subject
end
end
def numericality_matcher
double(:numericality_matcher, given_numeric_column?: nil)
end
def validating_even_number(options = {})
define_model :example, attr: :string do
validates_numericality_of :attr, { even: true }.merge(options)
end.new
end
def not_validating_even_number
define_model(:example, attr: :string).new
end
end

View file

@ -1,93 +0,0 @@
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::OddNumberMatcher do
subject { described_class.new(numericality_matcher, :attr) }
it_behaves_like 'a numerical submatcher'
context 'when the model has an odd validation' do
it 'matches' do
match = subject
expect(validating_odd_number).to match
end
end
context 'when the model does not have an odd validation' do
it 'rejects with an appropriate failure message' do
match = subject
assertion = lambda do
expect(not_validating_odd_number).to match
end
message = <<-MESSAGE.strip_heredoc
After setting :attr to "2", the matcher expected the Example to be
invalid, but it was valid instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'with custom validation message' do
it 'only accepts odd number values for that attribute with that message' do
expect(validating_odd_number(message: 'custom')).to subject.with_message(/custom/)
end
it 'fails odd number values for that attribute with another message' do
expect(validating_odd_number(message: 'custom')).not_to subject.with_message(/wrong/)
end
end
context 'asserting strict validation when validating strictly' do
it 'accepts' do
expect(validating_odd_number(strict: true)).to subject.strict
end
end
context 'asserting non-strict validation when validating strictly' do
it 'rejects' do
expect(validating_odd_number(strict: true)).not_to subject
end
end
context 'asserting strict validation when not validating strictly' do
it 'rejects' do
expect(validating_odd_number).not_to subject.strict
end
end
context 'qualified with on and validating with on' do
it 'accepts' do
expect(validating_odd_number(on: :customizable)).
to subject.on(:customizable)
end
end
context 'qualified with on but not validating with on' do
it 'accepts since the validation never considers a context' do
expect(validating_odd_number).to subject.on(:customizable)
end
end
context 'not qualified with on but validating with on' do
it 'rejects since the validation never runs' do
expect(validating_odd_number(on: :customizable)).
not_to subject
end
end
def numericality_matcher
double(:numericality_matcher, given_numeric_column?: nil)
end
def validating_odd_number(options = {})
define_model :example, attr: :string do
validates_numericality_of :attr, { odd: true }.merge(options)
end.new
end
def not_validating_odd_number
define_model(:example, attr: :string).new
end
end

View file

@ -1,93 +0,0 @@
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::OnlyIntegerMatcher do
subject { described_class.new(numericality_matcher, :attr) }
it_behaves_like 'a numerical submatcher'
context 'given an attribute that only allows integer values' do
it 'matches' do
match = subject
expect(validating_only_integer).to match
end
end
context 'given an attribute that only allows integer values with a custom validation message' do
it 'only accepts integer values for that attribute with that message' do
expect(validating_only_integer(message: 'custom')).to subject.with_message(/custom/)
end
it 'rejects integer values for that attribute with another message' do
expect(validating_only_integer(message: 'custom')).not_to subject.with_message(/wrong/)
end
end
context 'when the model does not have an only_integer validation' do
it 'rejects with an appropriate failure message' do
match = subject
assertion = lambda do
expect(not_validating_only_integer).to match
end
message = <<-MESSAGE
After setting :attr to "0.1", the matcher expected the Example to be
invalid, but it was valid instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'asserting strict validation when validating strictly' do
it 'accepts' do
expect(validating_only_integer(strict: true)).to subject.strict
end
end
context 'asserting non-strict validation when validating strictly' do
it 'rejects' do
expect(validating_only_integer(strict: true)).not_to subject
end
end
context 'asserting strict validation when not validating strictly' do
it 'rejects' do
expect(validating_only_integer).not_to subject.strict
end
end
context 'qualified with on and validating with on' do
it 'accepts' do
expect(validating_only_integer(on: :customizable)).
to subject.on(:customizable)
end
end
context 'qualified with on but not validating with on' do
it 'accepts since the validation never considers a context' do
expect(validating_only_integer).to subject.on(:customizable)
end
end
context 'not qualified with on but validating with on' do
it 'rejects since the validation never runs' do
expect(validating_only_integer(on: :customizable)).
not_to subject
end
end
def numericality_matcher
double(:numericality_matcher, given_numeric_column?: nil)
end
def validating_only_integer(options = {})
define_model :example, attr: :string do
validates_numericality_of :attr, { only_integer: true }.merge(options)
end.new
end
def not_validating_only_integer
define_model(:example, attr: :string).new
end
end