工厂女孩+ Mongoid嵌入式文件夹具(Factory Girl + Mongoid embedded documents in fixtures)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
工厂女孩+ Mongoid嵌入式文件夹具(Factory Girl + Mongoid embedded documents in fixtures)

假设您有以下mongoid文档:

class User include Mongoid::Document embeds_one :name end class UserName include Mongoid::Document field :first field :last_initial embedded_in :user end

你如何创建一个初始化嵌入式名字和最后一个字母的工厂女孩​​工厂? 另外你如何与embeds_many关系做到这embeds_many ?

Let’s say you have the following mongoid documents:

class User include Mongoid::Document embeds_one :name end class UserName include Mongoid::Document field :first field :last_initial embedded_in :user end

How do you create a factory girl factory which initializes the embedded first name and last initial? Also how would you do it with an embeds_many relationship?

最满意答案

我也在寻找这个,因为我在研究我偶然发现了很多代码,并将它们拼凑在一起(我希望有更好的文档),但这是我的代码的一部分。 地址是1..1的关系,电话是与事件1..n的关系。

factory :event do title 'Example Event' address { FactoryGirl.build(:address) } phones { [FactoryGirl.build(:phone1), FactoryGirl.build(:phone2)] } end factory :address do place 'foobar tower' street 'foobar st.' city 'foobar city' end factory :phone1, :class => :phone do code '432' number '1234567890' end factory :phone2, :class => :phone do code '432' number '0987654321' end

(抱歉,如果我不能提供我的链接,他们有点搞砸了)

I was also looking for this one and as I was researching I've stumbled on a lot of code and did pieced them all together (I wish there were better documents though) but here's my part of the code. Address is a 1..1 relationship and Phones is a 1..n relationship to events.

factory :event do title 'Example Event' address { FactoryGirl.build(:address) } phones { [FactoryGirl.build(:phone1), FactoryGirl.build(:phone2)] } end factory :address do place 'foobar tower' street 'foobar st.' city 'foobar city' end factory :phone1, :class => :phone do code '432' number '1234567890' end factory :phone2, :class => :phone do code '432' number '0987654321' end

(And sorry if I can't provide my links, they were kinda messed up)

更多推荐

本文发布于:2023-04-17 08:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e9cdb4a95119f9f5a8c715ec2a723fcc.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:夹具   嵌入式   工厂   女孩   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!