Rails 3 中的工厂女孩​​和nested_attributes

发布于 2024-11-02 23:04:29 字数 885 浏览 6 评论 0原文

我有 2 个模型,一个接受另一个的属性,我正在尝试找到一种巧妙的方法来使用 Factory Girl 为两个模型设置数据。

Class Booking
has_many :booking_items
accepts_nested_attributes_for :booking_items

Class BookingItem
belong_to :booking

我的工厂

Factory.define :booking do |f|
  f.bookdate Date.today+15.days
  f.association :space
  f.nights 2
  f.currency "EUR"
  f.booking_item_attributes Factory.build(:booking_item) # doesn't work
end

Factory.define :booking_item do |f|
  f.association :room
  f.bookdate Date.today
  f.people 2
  f.total_price 20
  f.association :booking
end

booking_spec

require "spec_helper"

describe Booking do

  before(:each) do
    @booking = Factory.create(:booking)
  end

  it "should be valid" do
    #needs children to be valid
    @booking.should be_valid
  end


end

我查看了 rdocs,但似乎找不到我要找的东西。

I have 2 models, one which accepts attributes for the other and I'm trying to find a clever way to use Factory girl to setup the data for both.

Class Booking
has_many :booking_items
accepts_nested_attributes_for :booking_items

Class BookingItem
belong_to :booking

my factory

Factory.define :booking do |f|
  f.bookdate Date.today+15.days
  f.association :space
  f.nights 2
  f.currency "EUR"
  f.booking_item_attributes Factory.build(:booking_item) # doesn't work
end

Factory.define :booking_item do |f|
  f.association :room
  f.bookdate Date.today
  f.people 2
  f.total_price 20
  f.association :booking
end

booking_spec

require "spec_helper"

describe Booking do

  before(:each) do
    @booking = Factory.create(:booking)
  end

  it "should be valid" do
    #needs children to be valid
    @booking.should be_valid
  end


end

I looked around the rdocs but couldn't seem to find what I was looking for.

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

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

发布评论

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

评论(1

断舍离 2024-11-09 23:04:29

如果我理解正确的话,你想要这样做,但是使用更简洁的语法:

booking_item = Factory(:booking_item, :people => 4)
booking = Factory(:booking, :booking_item => booking_item)

当然你可以像这样快捷方式:

def with_assocs factory, assocs_hashes = {}, attrs = {}
  assoc_models = Hash[ assocs_hash.map { |k, v| [k, Factory(k, v)] } ]
  Factory factory, attrs.merge(assoc_models)
end

并像这样使用:

@booking = with_assocs :booking, :booking_item => {:people => 3}
@booking.should be_valid

active_factory 插件 具有类似的工厂定义,它看起来像这样:

models { booking - booking_item(:people => 3) }
booking.should be_valid

不幸的是,我还没有实现与factory_girl 的集成。不过,如果您有兴趣,我们非常欢迎任何意见。

If I understood you correctly, you want to do this, but with terser syntax:

booking_item = Factory(:booking_item, :people => 4)
booking = Factory(:booking, :booking_item => booking_item)

Of cause you can shortcut it like this:

def with_assocs factory, assocs_hashes = {}, attrs = {}
  assoc_models = Hash[ assocs_hash.map { |k, v| [k, Factory(k, v)] } ]
  Factory factory, attrs.merge(assoc_models)
end

And use like this:

@booking = with_assocs :booking, :booking_item => {:people => 3}
@booking.should be_valid

In active_factory plugin with similar factory definitions it would look like this:

models { booking - booking_item(:people => 3) }
booking.should be_valid

Unfortunately I haven't yet implemented integration with factory_girl. Though if you interested any input is very welcome.

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