thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.51k stars 912 forks source link

feat: Add `have_delegated_type` matcher #1606

Closed matsales28 closed 7 months ago

matsales28 commented 9 months ago

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