mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Add failing tests for issue 1146
This commit is contained in:
parent
13b2338312
commit
da4e6ddd06
6 changed files with 121 additions and 59 deletions
|
@ -45,6 +45,9 @@ Metrics/LineLength:
|
|||
- "^[ ]*shared_context.+$"
|
||||
- "^[ ]*shared_examples_for.+$"
|
||||
- "^[ ]*it.+$"
|
||||
- "^[ ]*'.+?' => '.+?',?$"
|
||||
- "^[ ]*\".+?\" => \".+?\",?$"
|
||||
- "^[ ]*.+?: .+?$"
|
||||
Metrics/MethodLength:
|
||||
Max: 30
|
||||
Naming/AccessorMethodName:
|
||||
|
|
|
@ -128,20 +128,16 @@ module Shoulda
|
|||
options
|
||||
)
|
||||
default_translation_keys = [
|
||||
:"activemodel.errors.models.#{model_name}.attributes.#{attribute}.#{type}",
|
||||
:"activemodel.errors.models.#{model_name}.#{type}",
|
||||
:"activemodel.errors.messages.#{type}",
|
||||
:"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{type}",
|
||||
:"activerecord.errors.models.#{model_name}.#{type}",
|
||||
:"activerecord.errors.messages.#{type}",
|
||||
:"errors.attributes.#{attribute}.#{type}",
|
||||
:"errors.messages.#{type}",
|
||||
]
|
||||
primary_translation_key = [
|
||||
:activerecord,
|
||||
:errors,
|
||||
:models,
|
||||
model_name,
|
||||
:attributes,
|
||||
attribute,
|
||||
type,
|
||||
]
|
||||
primary_translation_key = default_translation_keys.shift
|
||||
translate_options =
|
||||
{ default: default_translation_keys }.merge(options)
|
||||
I18n.translate(primary_translation_key, translate_options)
|
||||
|
|
|
@ -6,6 +6,20 @@ module UnitTests
|
|||
example_group.include(self)
|
||||
end
|
||||
|
||||
def stubbing_translations(translations)
|
||||
stub_translations(translations)
|
||||
yield
|
||||
ensure
|
||||
I18n.backend.reload!
|
||||
I18n.backend.send(:init_translations)
|
||||
end
|
||||
|
||||
def stub_translations(translations)
|
||||
translations.each do |key, message|
|
||||
stub_translation(key, message)
|
||||
end
|
||||
end
|
||||
|
||||
def stub_translation(key_or_keys, message)
|
||||
keys = [key_or_keys].flatten.join('.').split('.')
|
||||
tree = keys.reverse.inject(message) { |data, key| { key => data } }
|
||||
|
|
|
@ -60,7 +60,7 @@ module UnitTests
|
|||
end
|
||||
|
||||
UnitTests::ModelCreationStrategies::ActiveModel.call(
|
||||
'Example',
|
||||
class_name,
|
||||
columns,
|
||||
options,
|
||||
&block
|
||||
|
|
|
@ -439,6 +439,36 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'not having been qualified' do
|
||||
it 'matches when the validation uses the default message' do
|
||||
builder = build_object_allowing(valid_values)
|
||||
expect_to_match_on_values(builder, valid_values)
|
||||
end
|
||||
|
||||
it 'matches when the validation uses the default message and it is overridden in i18n' do
|
||||
stubbing_translations(
|
||||
'errors.messages.inclusion' => 'some message',
|
||||
'errors.models.person.attributes.name.inclusion' => 'a different message',
|
||||
) do
|
||||
builder = build_object_allowing(
|
||||
valid_values,
|
||||
model_name: 'Person',
|
||||
attribute_name: :name,
|
||||
)
|
||||
expect_to_match_on_values(builder, valid_values)
|
||||
end
|
||||
end
|
||||
|
||||
it 'does not match when the validation is configured with an overridden message' do
|
||||
builder = build_object_allowing(
|
||||
valid_values,
|
||||
validation_options: { message: 'some message' },
|
||||
)
|
||||
|
||||
expect_not_to_match_on_values(builder, valid_values)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples_for 'it supports in_array' do |args|
|
||||
|
@ -448,6 +478,27 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
|
||||
define_method(:valid_values) { args.fetch(:possible_values) }
|
||||
|
||||
it_behaves_like 'it supports allow_nil', valid_values: possible_values
|
||||
it_behaves_like 'it supports allow_blank', valid_values: possible_values
|
||||
it_behaves_like 'it supports with_message', valid_values: possible_values
|
||||
|
||||
it_supports(
|
||||
'ignoring_interference_by_writer',
|
||||
tests: {
|
||||
reject_if_qualified_but_changing_value_interferes: {
|
||||
attribute_name: :attr,
|
||||
changing_values_with: :next_value,
|
||||
expected_message_includes: <<-MESSAGE.strip
|
||||
As indicated in the message above, :attr seems to be changing certain
|
||||
values as they are set, and this could have something to do with why
|
||||
this test is failing. If you've overridden the writer method for this
|
||||
attribute, then you may need to change it to make this test pass, or
|
||||
do something else entirely.
|
||||
MESSAGE
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
context 'when the record has no validations' do
|
||||
it 'passes when used in the negative' do
|
||||
builder = build_object
|
||||
|
@ -508,10 +559,6 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
expect(&assertion).to fail
|
||||
end
|
||||
|
||||
it_behaves_like 'it supports allow_nil', valid_values: possible_values
|
||||
it_behaves_like 'it supports allow_blank', valid_values: possible_values
|
||||
it_behaves_like 'it supports with_message', valid_values: possible_values
|
||||
|
||||
if active_model_3_2?
|
||||
context '+ strict' do
|
||||
context 'when the validation specifies strict' do
|
||||
|
@ -551,23 +598,6 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
end
|
||||
end
|
||||
|
||||
it_supports(
|
||||
'ignoring_interference_by_writer',
|
||||
tests: {
|
||||
reject_if_qualified_but_changing_value_interferes: {
|
||||
attribute_name: :attr,
|
||||
changing_values_with: :next_value,
|
||||
expected_message_includes: <<-MESSAGE.strip
|
||||
As indicated in the message above, :attr seems to be changing certain
|
||||
values as they are set, and this could have something to do with why
|
||||
this test is failing. If you've overridden the writer method for this
|
||||
attribute, then you may need to change it to make this test pass, or
|
||||
do something else entirely.
|
||||
MESSAGE
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
def expect_to_match_on_values(builder, values, &block)
|
||||
expect_to_match_in_array(builder, values, &block)
|
||||
end
|
||||
|
@ -590,6 +620,27 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
|
||||
define_method(:valid_values) { args.fetch(:possible_values) }
|
||||
|
||||
it_behaves_like 'it supports allow_nil', valid_values: possible_values
|
||||
it_behaves_like 'it supports allow_blank', valid_values: possible_values
|
||||
it_behaves_like 'it supports with_message', valid_values: possible_values
|
||||
|
||||
it_supports(
|
||||
'ignoring_interference_by_writer',
|
||||
tests: {
|
||||
reject_if_qualified_but_changing_value_interferes: {
|
||||
attribute_name: :attr,
|
||||
changing_values_with: :next_value,
|
||||
expected_message_includes: <<-MESSAGE.strip
|
||||
As indicated in the message above, :attr seems to be changing certain
|
||||
values as they are set, and this could have something to do with why
|
||||
this test is failing. If you've overridden the writer method for this
|
||||
attribute, then you may need to change it to make this test pass, or
|
||||
do something else entirely.
|
||||
MESSAGE
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
it 'does not match a record with no validations' do
|
||||
builder = build_object
|
||||
expect_not_to_match_on_values(builder, possible_values)
|
||||
|
@ -628,10 +679,6 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
)
|
||||
end
|
||||
|
||||
it_behaves_like 'it supports allow_nil', valid_values: possible_values
|
||||
it_behaves_like 'it supports allow_blank', valid_values: possible_values
|
||||
it_behaves_like 'it supports with_message', valid_values: possible_values
|
||||
|
||||
if active_model_3_2?
|
||||
context '+ strict' do
|
||||
context 'when the validation specifies strict' do
|
||||
|
@ -671,23 +718,6 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
end
|
||||
end
|
||||
|
||||
it_supports(
|
||||
'ignoring_interference_by_writer',
|
||||
tests: {
|
||||
reject_if_qualified_but_changing_value_interferes: {
|
||||
attribute_name: :attr,
|
||||
changing_values_with: :next_value,
|
||||
expected_message_includes: <<-MESSAGE.strip
|
||||
As indicated in the message above, :attr seems to be changing certain
|
||||
values as they are set, and this could have something to do with why
|
||||
this test is failing. If you've overridden the writer method for this
|
||||
attribute, then you may need to change it to make this test pass, or
|
||||
do something else entirely.
|
||||
MESSAGE
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
def expect_to_match_on_values(builder, range, &block)
|
||||
expect_to_match_in_range(builder, range, &block)
|
||||
end
|
||||
|
@ -837,8 +867,13 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
end
|
||||
end
|
||||
|
||||
def define_simple_model(attribute_name: :attr, column_options: {}, &block)
|
||||
define_model('Example', attribute_name => column_options, &block)
|
||||
def define_simple_model(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_options: {},
|
||||
&block
|
||||
)
|
||||
define_model(model_name, attribute_name => column_options, &block)
|
||||
end
|
||||
|
||||
def validation_matcher_scenario_args
|
||||
|
@ -865,8 +900,13 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
end
|
||||
end
|
||||
|
||||
def define_simple_model(attribute_name: :attr, column_options: {}, &block)
|
||||
define_active_model_class('Example', accessors: [attribute_name], &block)
|
||||
def define_simple_model(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_options: {},
|
||||
&block
|
||||
)
|
||||
define_active_model_class(model_name, accessors: [attribute_name], &block)
|
||||
end
|
||||
|
||||
def validation_matcher_scenario_args
|
||||
|
@ -923,12 +963,14 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
end
|
||||
|
||||
def build_object_with_generic_attribute(
|
||||
model_name: nil,
|
||||
attribute_name: :attr,
|
||||
validation_options: nil,
|
||||
value: nil,
|
||||
**other_options
|
||||
)
|
||||
model = define_model_validating_inclusion(
|
||||
model_name: model_name,
|
||||
attribute_name: attribute_name,
|
||||
validation_options: validation_options,
|
||||
**other_options
|
||||
|
@ -941,6 +983,7 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
end
|
||||
|
||||
def define_model_validating_inclusion(
|
||||
model_name: nil,
|
||||
attribute_name: :attr,
|
||||
column_type: :string,
|
||||
column_options: {},
|
||||
|
@ -949,11 +992,13 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|||
customize_model_class: -> (object) { }
|
||||
)
|
||||
column_options = { type: column_type, options: column_options }
|
||||
|
||||
define_simple_model(
|
||||
model_options = {
|
||||
model_name: model_name,
|
||||
attribute_name: attribute_name,
|
||||
column_options: column_options
|
||||
) do |model|
|
||||
}.compact
|
||||
|
||||
define_simple_model(model_options) do |model|
|
||||
if validation_options
|
||||
model.validates_inclusion_of(attribute_name, validation_options)
|
||||
end
|
||||
|
|
|
@ -42,6 +42,10 @@ RSpec.configure do |config|
|
|||
config.before(:all, type: :controller) do
|
||||
self.class.controller(ApplicationController) { }
|
||||
end
|
||||
|
||||
config.before(:suite) do
|
||||
I18n.backend.send(:init_translations)
|
||||
end
|
||||
end
|
||||
|
||||
ActiveSupport::Deprecation.behavior = :stderr
|
||||
|
|
Loading…
Add table
Reference in a new issue