使用factory_girl和mongoid来测试referenced_in/references_many

发布于 2024-10-24 09:10:05 字数 1893 浏览 4 评论 0原文

我正在尝试测试订阅服务的关联文档。每个订阅都嵌入一个帐户并引用一个计划。下面是各种代码:

帐户:

Factory.define :account, :class => Account do |a|
  a.subdomain 'test'
  a.agents { [ Factory.build(:user) ] }
  a.subscription { Factory.build(:free_subscription) }
end

订阅:

Factory.define :free_subscription, :class => Subscription do |s|
  s.started_at Time.now
  s.plan { Factory.build(:free_plan) }
end

计划:

Factory.define :free_plan, :class => Plan do |p|
  p.plan_name 'Free'
  p.cost 0
end

错误:

Mongoid::Errors::InvalidCollection: Access to the collection for Subscription is not allowed since it is an embedded document, please access a collection from the root document.

如果我注释掉将计划链接到订阅的行,则测试可以工作,但显然我无法测试订阅是否具有计划。

任何建议将不胜感激。

更新:

以下是模型:

class Account
  include Mongoid::Document

  field :company_name, :type => String
  field :subdomain, :type => String
  field :joined_at, :type => DateTime

  embeds_one :subscription

  accepts_nested_attributes_for :subscription

  before_create :set_joined_at_date

  private

  def set_joined_at_date
    self.joined_at = Time.now
  end
end

class Subscription
  include Mongoid::Document

  field :coupon_verified, :type => Boolean
  field :started_at, :type => DateTime

  referenced_in :plan
  embedded_in :account, :inverse_of => :subscription
end

class Plan
  include Mongoid::Document

  field :plan_name, :type => String
  field :cost, :type => Integer
  field :active_ticket_limit, :type => Integer
  field :agent_limit, :type => Integer
  field :company_limit, :type => Integer
  field :client_limit, :type => Integer
  field :sla_support, :type => Boolean
  field :report_support, :type => Boolean

  references_many :subscriptions
end

I am trying to test an associated document for a subscription service. Each subscription is embedded in an account and references a plan. Below is the various bits of code:

The account:

Factory.define :account, :class => Account do |a|
  a.subdomain 'test'
  a.agents { [ Factory.build(:user) ] }
  a.subscription { Factory.build(:free_subscription) }
end

The subscription:

Factory.define :free_subscription, :class => Subscription do |s|
  s.started_at Time.now
  s.plan { Factory.build(:free_plan) }
end

The plan:

Factory.define :free_plan, :class => Plan do |p|
  p.plan_name 'Free'
  p.cost 0
end

The error:

Mongoid::Errors::InvalidCollection: Access to the collection for Subscription is not allowed since it is an embedded document, please access a collection from the root document.

If I comment out the line that links the plan to the subscription then the tests work, but obviously I can't test that the subscription has a plan.

Any suggestions would be greatly appreciated.

UPDATE:

Here are the models:

class Account
  include Mongoid::Document

  field :company_name, :type => String
  field :subdomain, :type => String
  field :joined_at, :type => DateTime

  embeds_one :subscription

  accepts_nested_attributes_for :subscription

  before_create :set_joined_at_date

  private

  def set_joined_at_date
    self.joined_at = Time.now
  end
end

class Subscription
  include Mongoid::Document

  field :coupon_verified, :type => Boolean
  field :started_at, :type => DateTime

  referenced_in :plan
  embedded_in :account, :inverse_of => :subscription
end

class Plan
  include Mongoid::Document

  field :plan_name, :type => String
  field :cost, :type => Integer
  field :active_ticket_limit, :type => Integer
  field :agent_limit, :type => Integer
  field :company_limit, :type => Integer
  field :client_limit, :type => Integer
  field :sla_support, :type => Boolean
  field :report_support, :type => Boolean

  references_many :subscriptions
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-10-31 09:10:05

您需要创建一个包含订阅的帐户才能使其有效。

Factory.define :free_subscription, :class => Subscription do |s|
  s.started_at Time.now
  s.plan { Factory.build(:free_plan) }
  s.account { Factory(:account) }
end

You need to create an account with the subscription for it to be valid.

Factory.define :free_subscription, :class => Subscription do |s|
  s.started_at Time.now
  s.plan { Factory.build(:free_plan) }
  s.account { Factory(:account) }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文