shoulda-matchers icon indicating copy to clipboard operation
shoulda-matchers copied to clipboard

feat: Add `have_delegated_type` matcher

Open matsales28 opened this issue 1 year ago • 0 comments

On this PR, we're adding the matcher for the delegated_type macro, which is basically a belongs_to polymorphic association where you define the possible classes the polymorphic relation can act as.

Currently, it is hard to check those possible classes as Rails does not expose them in any way, so we need to check if the methods for those classes are defined, which is not ideal.

class Vehicle < ActiveRecord::Base
  delegated_type :drivable, types: %w(Car Truck)
end

# Usage 
RSpec.describe Vehicle, type: :model do
 it { should have_delegated_type(:drivable) }
end

matsales28 avatar Jan 19 '24 21:01 matsales28