Home > Software design >  How to correctly check if a result set is ActiveRecord::Associations::CollectionProxy
How to correctly check if a result set is ActiveRecord::Associations::CollectionProxy

Time:02-10

I want to check if my result set is of type ActiveRecord::Associations::CollectionProxy

for example my models are as follows

class Dashboard < ApplicationRecord
  has_and_belongs_to_many :organisations
end
class Organisation < ApplicationRecord
  has_and_belongs_to_many :dashboards
end

When i do organisation.dashboards i want to check if its ActiveRecord::Associations::CollectionProxy

but something like the below does not work.

expect(organisation.dashboards).to be(ActiveRecord::Associations::CollectionProxy)

Any help in this would be really great, Thanks.

CodePudding user response:

You can use rspec Type matchers here

expect(obj).to be_kind_of(type): calls obj.kind_of?(type), which returns true if type is in obj's class hierarchy or is a module and is included in a class in obj's class hierarchy.

expect(organisation.dashboards).to be_kind_of(ActiveRecord::Associations::CollectionProxy)

OR

expect(organisation.dashboards).to be_a_kind_of(ActiveRecord::Associations::CollectionProxy)

OR

expect(organisation.dashboards).to be_a(ActiveRecord::Associations::CollectionProxy)
  • Related