diff --git a/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb b/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb index dc28f009e..414002454 100644 --- a/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb @@ -59,7 +59,7 @@ def normalize_value when String, Symbol { active: true, column: counter_cache.to_s } when Hash - { active: true }.merge(counter_cache) + { active: true, column: nil }.merge(counter_cache) else raise ArgumentError, 'Invalid counter_cache option' end 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 7027b7222..daea71cdf 100644 --- a/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb @@ -113,6 +113,28 @@ expect(belonging_to_parent).not_to belong_to(:parent).counter_cache end + if rails_version > 7.1 + it 'accepts :counter_cache with a hash' do + expect(belonging_to_parent(counter_cache: { active: true })). + to belong_to(:parent).counter_cache + end + + it 'accepts :counter_cache with active false when passed' do + expect(belonging_to_parent(counter_cache: { active: false })). + to belong_to(:parent).counter_cache(active: false) + end + + it 'rejects :counter_cache with active false when mismatch' do + expect(belonging_to_parent(counter_cache: { active: true })). + not_to belong_to(:parent).counter_cache(active: false) + end + + it 'rejects :counter_cache with when column mismatch' do + expect(belonging_to_parent(counter_cache: { column: :attribute_count })). + not_to belong_to(:parent).counter_cache(true) + end + end + it 'accepts an association with a valid :inverse_of option' do expect(belonging_to_with_inverse(:parent, :children)). to belong_to(:parent).inverse_of(:children)