diff --git a/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb b/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb index da3a2a74f..92e1f394b 100644 --- a/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +++ b/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb @@ -15,10 +15,8 @@ def initialize(reflection) def associated_class associated_class = reflection.klass - if subject.strict_loading_by_default - unless reflection.options[:strict_loading] == false - reflection.options[:strict_loading] = true - end + if subject.strict_loading_by_default && !(reflection.options[:strict_loading] == false) + reflection.options[:strict_loading] = true end associated_class diff --git a/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb index 1b9eedede..a5754298e 100644 --- a/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb @@ -1200,6 +1200,41 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {}) end context 'when the application is configured with strict_loading disabled by default' do + it 'accepts an association with a matching :strict_loading option' do + with_strict_loading_by_default_disabled do + expect(having_many_children). + to have_many(:children).strict_loading(false) + end + end + + it 'rejects an association with a non-matching :strict_loading option without explicit value with the correct message' do + with_strict_loading_by_default_disabled do + message = [ + 'Expected Parent to have a has_many association called children ', + '(children should have strict_loading set to true)', + ].join + + expect { + expect(having_many_children). + to have_many(:children).strict_loading + }.to fail_with_message(message) + end + end + + it 'rejects an association with a non-matching :strict_loading option with the correct message' do + with_strict_loading_by_default_disabled do + message = [ + 'Expected Parent to have a has_many association called children ', + '(children should have strict_loading set to true)', + ].join + + expect { + expect(having_many_children). + to have_many(:children).strict_loading(true) + }.to fail_with_message(message) + end + end + context 'when the association is configured with a strict_loading constraint' do context 'when qualified with strict_loading(true)' do it 'accepts an association with a matching :strict_loading option' do @@ -1239,7 +1274,7 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {}) end end - it 'rejects an association with a matching :strict_loading option without explicit value with the correct message' do + it 'rejects an association with a non-matching :strict_loading option without explicit value with the correct message' do with_strict_loading_by_default_disabled do message = [ 'Expected Parent to have a has_many association called children ', @@ -1370,6 +1405,34 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {}) end context 'when the application is configured with strict_loading enabled by default' do + it 'accepts an association with a matching :strict_loading option' do + with_strict_loading_by_default_enabled do + expect(having_many_children). + to have_many(:children).strict_loading(true) + end + end + + it 'accepts an association with a matching :strict_loading option without explicit value' do + with_strict_loading_by_default_enabled do + expect(having_many_children). + to have_many(:children).strict_loading + end + end + + it 'rejects an association with a non-matching :strict_loading option with the correct message' do + with_strict_loading_by_default_enabled do + message = [ + 'Expected Parent to have a has_many association called children ', + '(children should have strict_loading set to false)', + ].join + + expect { + expect(having_many_children). + to have_many(:children).strict_loading(false) + }.to fail_with_message(message) + end + end + context 'when the association is configured with a strict_loading constraint' do context 'when qualified with strict_loading(true)' do it 'accepts an association with a matching :strict_loading option' do @@ -1409,7 +1472,7 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {}) end end - it 'rejects an association with a matching :strict_loading option without explicit value with the correct message' do + it 'rejects an association with a non-matching :strict_loading option without explicit value with the correct message' do with_strict_loading_by_default_enabled do message = [ 'Expected Parent to have a has_many association called children ',