如何正确检查结果集是否为 ActiveRecord::Associations::CollectionProxy

How to correctly check if a result set is ActiveRecord::Associations::CollectionProxy

我想检查我的结果集是否属于 ActiveRecord::Associations::CollectionProxy

类型

比如我的模型如下

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

当我 organisation.dashboards 我想检查它是否 ActiveRecord::Associations::CollectionProxy

但是像下面这样的东西不起作用。

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

这方面的任何帮助都非常好,谢谢。

你可以在这里使用rspecType matchers

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)

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

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