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

fix: has_many without validate option returns false instead of true (#1378)

This commit is contained in:
Pedro Paiva 2020-11-19 07:43:58 -03:00 committed by GitHub
parent bf41204490
commit 8772cd41a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View file

@ -6,6 +6,11 @@ module Shoulda
class OptionVerifier class OptionVerifier
delegate :reflection, to: :reflector delegate :reflection, to: :reflector
DEFAULT_VALUE_OF_OPTIONS = {
has_many: {
validate: true,
},
}.freeze
RELATION_OPTIONS = [:conditions, :order].freeze RELATION_OPTIONS = [:conditions, :order].freeze
def initialize(reflector) def initialize(reflector)
@ -55,7 +60,7 @@ module Shoulda
if respond_to?(method_name, true) if respond_to?(method_name, true)
__send__(method_name) __send__(method_name)
else else
reflection.options[name] actual_value_for_option(name)
end end
end end
end end
@ -116,6 +121,16 @@ module Shoulda
def actual_value_for_class_name def actual_value_for_class_name
reflector.associated_class reflector.associated_class
end end
def actual_value_for_option(name)
option_value = reflection.options[name]
if option_value.nil?
DEFAULT_VALUE_OF_OPTIONS.dig(reflection.macro, name)
else
option_value
end
end
end end
end end
end end

View file

@ -1177,20 +1177,40 @@ Expected Parent to have a has_many association called children through conceptio
end end
context 'validate' do context 'validate' do
it 'accepts when the :validate option matches' do it 'accepts validate(false) when the :validate option is false' do
expect(having_many_children(validate: false)).to have_many(:children).validate(false) expect(having_many_children(validate: false)).to have_many(:children).validate(false)
end end
it 'rejects when the :validate option does not match' do it 'accepts validate(true) when the :validate option is true' do
expect(having_many_children(validate: true)).to have_many(:children).validate(true)
end
it 'rejects validate(false) when the :validate option is true' do
expect(having_many_children(validate: true)).not_to have_many(:children).validate(false) expect(having_many_children(validate: true)).not_to have_many(:children).validate(false)
end end
it 'rejects validate(true) when the :validate option is false' do
expect(having_many_children(validate: false)).not_to have_many(:children).validate(true)
end
it 'assumes validate() means validate(true)' do it 'assumes validate() means validate(true)' do
expect(having_many_children(validate: true)).to have_many(:children).validate
end
it 'rejects validate() when :validate option is false' do
expect(having_many_children(validate: false)).not_to have_many(:children).validate expect(having_many_children(validate: false)).not_to have_many(:children).validate
end end
it 'matches validate(false) to having no validate option specified' do it 'rejects validate(false) when no :validate option was specified' do
expect(having_many_children).to have_many(:children).validate(false) expect(having_many_children).not_to have_many(:children).validate(false)
end
it 'accepts validate(true) when no :validate option was specified' do
expect(having_many_children).to have_many(:children).validate(true)
end
it 'accepts validate() when no :validate option was specified' do
expect(having_many_children).to have_many(:children).validate
end end
end end