工厂女孩:你如何建造一个与模型无关的工厂?

时间:2011-06-29 16:17:22

标签: ruby-on-rails factory-bot

我只需要信用卡和地址等属性的哈希值。 示例:

Factory.define :credit_card, :class => Object do |c|
  c.first_name "Alice"
  c.last_name "Liddel"
  c.month "May"
  c.year { Time.now.year + 1 }
  c.number "1234567812345678"
  c.type "Visa"
  c.verification_value "123"
end
显然,对象没有任何属性,我没有credit_cord对象......我只需要一个标准的信用卡骨架。

1 个答案:

答案 0 :(得分:1)

使用工厂女孩的优势是什么?辅助方法怎么样:

def credit_card(attrs = {})
   {
     :first_name => "Alice",
     ...,
     :verification_value => "123"
   }.with_indifferent_access.merge(attrs)
end

credit_card :first_name => "Linda"   # returns { :first_name => "Linda", :last_name => "Liddel", ... }