has_many同时尊重factory_girl中的构建策略

时间:2009-10-01 21:17:32

标签: ruby-on-rails ruby unit-testing factory-bot

情况

# Models
class User < ActiveRecord::Base
  has_many :items 
end 

class Items < ActiveRecord::Base
  belongs_to :user 
  validates_presence_of :user_id 
end 

# Factories
Factory.define(:user) do |u| 
  u.name "foo" 
end 

Factory.define(:user_with_items, :parent => :user) do |u| 
  u.items {|items| [items.association(:item), items.association(:item)]} 
end

Factory.define(:item) do |i| 
  i.color "red" 
end 

Factory.define(:item_with_user, :parent => :user) do |i| 
  i.association(:user) 
end

问题

如果您运行@user = Factory(:user_with_items),则@user.items包含这两项。问题是项目与数据库中的用户无关。如果你重新加载关联@user.items(true),那么你会得到一个空数组。我知道你可以建造它们 手动或自己创建辅助方法来构建对象图,但我想避免这种情况。

问题

所以,我的问题是如何在尊重构建策略的同时在factory_girl中建立一个has_many关系?

3 个答案:

答案 0 :(得分:18)

我用继承和所有这些正确地写了它。我的提交已合并到herehere

现在在FactoryGirl 1.2.3,woot!

答案 1 :(得分:11)

我最终修补工厂女孩以允许after_buildafter_create回调。

实施

Factory.class_eval do
  def run (proxy_class, overrides) #:nodoc:
    proxy = proxy_class.new(build_class)
    proxy.callbacks = @callbacks
    overrides = symbolize_keys(overrides)
    overrides.each {|attr, val| proxy.set(attr, val) }
    passed_keys = overrides.keys.collect {|k| Factory.aliases_for(k) }.flatten
    @attributes.each do |attribute|
      unless passed_keys.include?(attribute.name)
        attribute.add_to(proxy)
      end
    end
    proxy.result
  end

  def after_create(&block)
    @callbacks ||= {}
    @callbacks[:after_create] = block
  end

  def after_build(&block)
    @callbacks ||= {}
    @callbacks[:after_build] = block
  end
end

Factory::Proxy.class_eval do
  attr_accessor :callbacks

  def run_callback(name)
    callbacks && callbacks[name] && callbacks[name].call(@instance)
  end
end

Factory::Proxy::Build.class_eval do
  def result
    run_callback(:after_build)
    @instance
  end
end

Factory::Proxy::Create.class_eval do
  def result
    run_callback(:after_build)
    @instance.save!
    run_callback(:after_create)
    @instance
  end
end

这可能是一个邪恶的双胞胎或只是你需要的扩展。

使用示例

# Models
class User < ActiveRecord::Base
  has_many :items
end

class Items < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :user_id
end

# Factories
Factory.define(:user) do |u|
  u.name "foo"
end

Factory.define(:user_with_items, :parent => :user) do |u|
  u.after_build do |o|
    o.items = [Factory.build(:item, :user => o), Factory.build(:item, :user => o)]
  end
end

Factory.define(:item) do |i|
  i.color "red"
end

Factory.define(:item_with_user, :parent => :user) do |i|
  i.association(:user)
end

# Run
user = Factory(:user_with_items)
user.items(true) # Shows the two saved items

希望这可以帮助将来的某个人。我可能会尝试将这个提交给thinkbot上的那些人,但是他们已经在他们的bug跟踪器中有关于该主题的几张过时的票。

答案 2 :(得分:5)

我通常喜欢将构建和创建分开,所以我仍然可以在不进入数据库的情况下构建对象。

Factory.define(:user_with_items, :parent => :user) do |u|
  u.after_build do |u|
    u.items = (1..2).map {Factory.build(:item, :user => u)}
  end
  u.after_create do |u|
    u.items.each {|i| i.save!}
  end
end