小编Jam*_*eod的帖子

使用带有mongoid的factory_girl来测试referenced_in/references_many

我正在尝试为订阅服务测试相关文档.每个订阅都嵌入在一个帐户中并引用一个计划.以下是各种代码:

帐户:

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

订阅:

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

计划:

Factory.define :free_plan, :class => Plan do |p|
  p.plan_name 'Free'
  p.cost 0
end
Run Code Online (Sandbox Code Playgroud)

错误:

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.
Run Code Online (Sandbox Code Playgroud)

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

任何建议将不胜感激.

更新:

以下是模型:

class …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails mongoid ruby-on-rails-3 factory-bot

0
推荐指数
1
解决办法
3772
查看次数