工厂女孩+ Mongoid 在装置中嵌入文档

发布于 2024-12-06 18:46:47 字数 312 浏览 0 评论 0原文

假设您有以下 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 关系来做到这一点?

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?

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

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

发布评论

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

评论(2

风渺 2024-12-13 18:46:47

我也在寻找这个,当我研究时,我偶然发现了很多代码,并将它们全部拼凑在一起(尽管我希望有更好的文档),但这是我的代码部分。地址与事件是 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)

給妳壹絲溫柔 2024-12-13 18:46:47

这是一个允许您动态定义嵌入对象数量的解决方案:

FactoryGirl.define do
  factory :profile do
    name 'John Doe'
    email '[email protected]'
    user

    factory :profile_with_notes do
      ignore do
        notes_count 2
      end

      after(:build) do |profile, evaluator|
        evaluator.notes_count.times do
          profile.notes.build(FactoryGirl.attributes_for(:note))
        end
      end
    end
  end
end

这允许您调用 FactoryGirl.create(:profile_with_notes) 并获取两个嵌入注释,或者调用 FactoryGirl.create( :profile_with_notes,notes_count: 5) 并获得五个嵌入式注释。

Here is a solution that allows you to dynamically define the number of embedded objects:

FactoryGirl.define do
  factory :profile do
    name 'John Doe'
    email '[email protected]'
    user

    factory :profile_with_notes do
      ignore do
        notes_count 2
      end

      after(:build) do |profile, evaluator|
        evaluator.notes_count.times do
          profile.notes.build(FactoryGirl.attributes_for(:note))
        end
      end
    end
  end
end

This allows you to call FactoryGirl.create(:profile_with_notes) and get two embedded notes, or call FactoryGirl.create(:profile_with_notes, notes_count: 5) and get five embedded notes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文