42

我正在使用 Rails 2.2 项目来更新它。我正在用工厂替换现有的固定装置(使用 factory_girl)并且遇到了一些问题。问题在于表示具有查找数据的表的模型。当我使用具有相同产品类型的两种产品创建购物车时,每个创建的产品都在重新创建相同的产品类型。此错误来自对 ProductType 模型的唯一验证。

问题演示

这是来自一个单元测试,我在其中创建了一个购物车并将其拼凑在一起。我必须这样做才能解决问题。不过,这仍然说明了问题。我会解释的。

cart = Factory(:cart)
cart.cart_items = [Factory(:cart_item, 
                           :cart => cart, 
                           :product => Factory(:added_users_product)),
                   Factory(:cart_item, 
                           :cart => cart, 
                           :product => Factory(:added_profiles_product))]

添加的两个产品类型相同,创建每个产品时都会重新创建产品类型并创建副本。

生成的错误是:“ActiveRecord::RecordInvalid:验证失败:名称已被采用,代码已被采用”

解决方法

此示例的解决方法是覆盖正在使用的产品类型并传入特定实例,因此仅使用一个实例。“add_product_type”被提前获取并为每个购物车项目传递。

cart = Factory(:cart)
prod_type = Factory(:add_product_type)   #New
cart.cart_items = [Factory(:cart_item,
                           :cart => cart,
                           :product => Factory(:added_users_product,
                                               :product_type => prod_type)), #New
                   Factory(:cart_item,
                           :cart => cart,
                           :product => Factory(:added_profiles_product,
                                               :product_type => prod_type))] #New

问题

将 factory_girl 与“选择列表”类型的关联一起使用的最佳方式是什么?

我希望工厂定义包含所有内容,而不必在测试中组装它,尽管我可以忍受它。

背景和额外细节

工厂/product.rb

# Declare ProductTypes

Factory.define :product_type do |t|
  t.name "None"
  t.code "none"
end

Factory.define :sub_product_type, :parent => :product_type do |t|
  t.name "Subscription"
  t.code "sub"
end

Factory.define :add_product_type, :parent => :product_type do |t|
  t.name "Additions"
  t.code "add"
end

# Declare Products

Factory.define :product do |p|
  p.association :product_type, :factory => :add_product_type
  #...
end

Factory.define :added_profiles_product, :parent => :product do |p|
  p.association :product_type, :factory => :add_product_type
  #...
end

Factory.define :added_users_product, :parent => :product do |p|
  p.association :product_type, :factory => :add_product_type
  #...
end

ProductType 的“代码”的目的是让应用程序可以赋予它们特殊的含义。ProductType 模型如下所示:

class ProductType < ActiveRecord::Base
  has_many :products

  validates_presence_of :name, :code
  validates_uniqueness_of :name, :code
  #...
end

工厂/cart.rb

# Define Cart Items

Factory.define :cart_item do |i|
  i.association :cart
  i.association :product, :factory => :test_product
  i.quantity 1
end

Factory.define :cart_item_sub, :parent => :cart_item do |i|
  i.association :product, :factory => :year_sub_product
end

Factory.define :cart_item_add_profiles, :parent => :cart_item do |i|
  i.association :product, :factory => :add_profiles_product
end

# Define Carts

# Define a basic cart class. No cart_items as it creates dups with lookup types.
Factory.define :cart do |c|
  c.association :account, :factory => :trial_account
end

Factory.define :cart_with_two_different_items, :parent => :cart do |o|
  o.after_build do |cart|
    cart.cart_items = [Factory(:cart_item, 
                               :cart => cart, 
                               :product => Factory(:year_sub_product)),
                       Factory(:cart_item, 
                               :cart => cart, 
                               :product => Factory(:added_profiles_product))]
  end
end

当我尝试使用相同产品类型的两个项目定义购物车时,我收到上述相同的错误。

Factory.define :cart_with_two_add_items, :parent => :cart do |o|
  o.after_build do |cart|
    cart.cart_items = [Factory(:cart_item,
                               :cart => cart,
                               :product => Factory(:added_users_product)),
                       Factory(:cart_item,
                               :cart => cart,
                               :product => Factory(:added_profiles_product))]
  end
end
4

10 回答 10

48

仅供参考,您也可以initialize_with在工厂内使用宏并检查对象是否已经存在,然后不要再次创建它。使用 lambda 的解决方案(它很棒,但是!)正在复制 find_or_create_by 中已经存在的逻辑。这也适用于通过关联工厂创建 :league 的关联。

FactoryGirl.define do
  factory :league, :aliases => [:euro_cup] do
    id 1
    name "European Championship"
    rank 30
    initialize_with { League.find_or_create_by_id(id)}
  end
end
于 2012-07-10T07:23:20.957 回答
31

我遇到了同样的问题,并在我的工厂文件顶部添加了一个实现单例模式的 lambda,如果自上一轮测试/规范以来已清除数据库,它也会重新生成模型:

saved_single_instances = {}
#Find or create the model instance
single_instances = lambda do |factory_key|
  begin
    saved_single_instances[factory_key].reload
  rescue NoMethodError, ActiveRecord::RecordNotFound  
    #was never created (is nil) or was cleared from db
    saved_single_instances[factory_key] = Factory.create(factory_key)  #recreate
  end

  return saved_single_instances[factory_key]
end

然后,使用您的示例工厂,您可以使用 factory_girl 惰性属性来运行 lambda

Factory.define :product do |p|
  p.product_type  { single_instances[:add_product_type] }
  #...this block edited as per comment below
end

瞧!

于 2010-08-25T18:27:08.967 回答
3

编辑:
在这个答案的底部看到一个更清洁的解决方案。

原始答案:
这是我创建 FactoryGirl 单例关联的解决方案:

FactoryGirl.define do
  factory :platform do
    name 'Foo'
  end

  factory :platform_version do
    name 'Bar'
    platform {
      if Platform.find(:first).blank?
        FactoryGirl.create(:platform)
      else
        Platform.find(:first)
      end
    }
  end
end

你称之为例如:

And the following platform versions exists:
  | Name     |
  | Master   |
  | Slave    |
  | Replica  |

这样,所有 3 个平台版本都将具有相同的平台“Foo”,即单例。

如果你想保存一个数据库查询,你可以这样做:

platform {
  search = Platform.find(:first)
  if search.blank?
    FactoryGirl.create(:platform)
  else
    search
  end
}

您可以考虑将单例关联设为特征:

factory :platform_version do
  name 'Bar'
  platform

  trait :singleton do
    platform {
      search = Platform.find(:first)
      if search.blank?
        FactoryGirl.create(:platform)
      else
        search
      end
    }
  end

  factory :singleton_platform_version, :traits => [:singleton]
end

如果您想设置超过 1 个平台,并且有不同的平台版本集,您可以制作更具体的不同特征,即:

factory :platform_version do
  name 'Bar'
  platform

  trait :singleton do
    platform {
      search = Platform.find(:first)
      if search.blank?
        FactoryGirl.create(:platform)
      else
        search
      end
    }
  end

  trait :newfoo do
    platform {
      search = Platform.find_by_name('NewFoo')
      if search.blank?
        FactoryGirl.create(:platform, :name => 'NewFoo')
      else
        search
      end
    }
  end

  factory :singleton_platform_version, :traits => [:singleton]
  factory :newfoo_platform_version, :traits => [:newfoo]
end

希望这对那里的一些人有用。

编辑:
在上面提交我的原始解决方案后,我又看了一下代码,并找到了一种更简洁的方法:您不在工厂中定义特征,而是在调用测试步骤时指定关联。

制作正规工厂:

FactoryGirl.define do
  factory :platform do
    name 'Foo'
  end

  factory :platform_version do
    name 'Bar'
    platform
  end
end

现在您使用指定的关联调用测试步骤:

And the following platform versions exists:
  | Name     | Platform     |
  | Master   | Name: NewFoo |
  | Slave    | Name: NewFoo |
  | Replica  | Name: NewFoo |

当这样做时,平台'NewFoo'的创建使用'find_or_create_by'功能,所以第一次调用创建平台,接下来的2次调用找到已经创建的平台。

这样,所有 3 个平台版本都将具有相同的平台“NewFoo”,您可以根据需要创建任意多的平台版本集。

我认为这是一个非常干净的解决方案,因为您保持工厂清洁,并且您甚至可以让测试步骤的读者看到这 3 个平台版本都具有相同的平台。

于 2011-12-01T14:44:46.170 回答
2

简短的回答是,“不”,工厂女孩没有更清洁的方法来做到这一点。我似乎在工厂女孩论坛上验证了这一点。

但是,我为自己找到了另一个答案。它涉及另一种解决方法,但使一切变得更清洁。

这个想法是更改代表查找表的模型以在缺少时创建所需的条目。这没关系,因为代码期望存在特定的条目。这是修改后的模型的示例。

class ProductType < ActiveRecord::Base
  has_many :products

  validates_presence_of :name, :code
  validates_uniqueness_of :name, :code

  # Constants defined for the class.
  CODE_FOR_SUBSCRIPTION = "sub"
  CODE_FOR_ADDITION = "add"

  # Get the ID for of the entry that represents a trial account status.
  def self.id_for_subscription
    type = ProductType.find(:first, :conditions => ["code = ?", CODE_FOR_SUBSCRIPTION])
    # if the type wasn't found, create it.
    if type.nil?
      type = ProductType.create!(:name => 'Subscription', :code => CODE_FOR_SUBSCRIPTION)
    end
    # Return the loaded or created ID
    type.id
  end

  # Get the ID for of the entry that represents a trial account status.
  def self.id_for_addition
    type = ProductType.find(:first, :conditions => ["code = ?", CODE_FOR_ADDITION])
    # if the type wasn't found, create it.
    if type.nil?
      type = ProductType.create!(:name => 'Additions', :code => CODE_FOR_ADDITION)
    end
    # Return the loaded or created ID
    type.id
  end
end

“id_for_addition”的静态类方法将加载模型和ID,如果找到,如果没有找到它会创建它。

缺点是“id_for_addition”方法可能不清楚它的名称。这可能需要改变。对正常使用的唯一其他代码影响是额外测试以查看是否找到模型。

这意味着可以像这样更改用于创建产品的工厂代码......

Factory.define :added_users_product, :parent => :product do |p|
  #p.association :product_type, :factory => :add_product_type
  p.product_type_id { ProductType.id_for_addition }
end

这意味着修改后的工厂代码看起来像这样......

Factory.define :cart_with_two_add_items, :parent => :cart do |o|
  o.after_build do |cart|
    cart.cart_items = [Factory(:cart_item_add_users, :cart => cart),
                       Factory(:cart_item_add_profiles, :cart => cart)]
  end
end

这正是我想要的。我现在可以清楚地表达我的工厂和测试代码。

这种方法的另一个好处是查找表数据不需要在迁移中播种或填充。它将自行处理测试数据库和生产。

于 2010-01-13T17:37:31.170 回答
2

当单身人士被引入工厂时,这些问题将被消除 - 目前在 - http://github.com/roderickvd/factory_girl/tree/singletons 问题 - http://github.com/thoughtbot/factory_girl/issues#issue/ 16

于 2010-01-19T19:34:45.327 回答
2

我也有类似的情况。我最终使用我的seeds.rb 来定义单例,然后要求spec_helper.rb 中的seeds.rb 将对象创建到测试数据库中。然后我可以在工厂中搜索适当的对象。

分贝/种子.rb

RegionType.find_or_create_by_region_type('community')
RegionType.find_or_create_by_region_type('province')

规范/spec_helper.rb

require "#{Rails.root}/db/seeds.rb"

规格/工厂.rb

FactoryGirl.define do
  factory :region_community, class: Region do
    sequence(:name) { |n| "Community#{n}" }
    region_type { RegionType.find_by_region_type("community") }
  end
end
于 2012-03-04T19:26:24.693 回答
1

我遇到了同样的问题,我认为它与此处引用的问题相同: http://groups.google.com/group/factory_girl/browse_frm/thread/68947290d1819952/ef22581f4cd05aa9?tvc=1&q=associations+ validates_uniqueness_of#ef22581f4cd05aa9

我认为您的解决方法可能是解决问题的最佳方法。

于 2010-01-07T04:57:18.217 回答
1

受此处答案的启发,我发现@Jonas Bang 的建议最符合我的需求。以下是 2016 年年中对我有用的方法(FactoryGirl v4.7.0,Rails 5rc1):

FactoryGirl.define do
  factory :platform do
    name 'Foo'
  end

  factory :platform_version do
    name 'Bar'
    platform { Platform.first || create(:platform) }
  end
end

使用它创建具有相同平台引用的四个 platform_version 的示例:

FactoryGirl.create :platform_version
FactoryGirl.create :platform_version, name: 'Car'
FactoryGirl.create :platform_version, name: 'Dar'

=>

-------------------
 platform_versions
-------------------
 name | platform
------+------------
 Bar  | Foo
 Car  | Foo
 Dar  | Foo

如果您在不同的平台上需要“Dar”:

FactoryGirl.create :platform_version
FactoryGirl.create :platform_version, name: 'Car'
FactoryGirl.create :platform_version, name: 'Dar', platform: create(:platform, name: 'Goo')

=>

-------------------
 platform_versions
-------------------
 name | platform
------+------------
 Bar  | Foo
 Car  | Foo
 Dar  | Goo

感觉就像两全其美,而不会使 factory_girl 变形太远。

于 2016-06-13T06:00:34.567 回答
0

我想我至少找到了一种更清洁的方法。

我喜欢联系 ThoughtBot 以获得推荐的“官方”解决方案的想法。目前,这运作良好。

我只是将在测试代码中执行此操作的方法与在工厂定义中执行所有操作相结合。

Factory.define :cart_with_two_add_items, :parent => :cart do |o|
  o.after_build do |cart|
    prod_type = Factory(:add_product_type) # Define locally here and reuse below

    cart.cart_items = [Factory(:cart_item,
                               :cart => cart,
                               :product => Factory(:added_users_product,
                                                   :product_type => prod_type)),
                       Factory(:cart_item,
                               :cart => cart,
                               :product => Factory(:added_profiles_product,
                                                   :product_type => prod_type))]
  end
end

def test_cart_with_same_item_types
  cart = Factory(:cart_with_two_add_items)
  # ... Do asserts
end

如果我找到更好的解决方案,我会更新。

于 2010-01-07T16:05:01.703 回答
0

也许您可以尝试使用 factory_girl 的序列作为产品类型名称和代码字段?对于大多数测试,我猜您不会关心产品类型的代码是“code 1”还是“sub”,而对于您关心的那些,您始终可以明确指定。

Factory.sequence(:product_type_name) { |n| "ProductType#{n}" }
Factory.sequence(:product_type_code) { |n| "prod_#{n}" }        

Factory.define :product_type do |t|
  t.name { Factory.next(:product_type_name) }
  t.code { Factory.next(:product_type_code) }
end 
于 2010-01-13T23:55:53.453 回答