2011-06-29 9 views
1

私は、クレジットカードや住所などの属性のハッシュが必要です。 例:工場の女の子:モデルに縛られていない工場をどうやって作っていますか?

明らか
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

これは工場の女の子を使用する利点は何ですか?ヘルパーメソッドについてはどうですか:

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", ... } 
​​
+0

繰り返されるデータの生成の均一性について。工場(:what_im_making、attrs) – NullVoxPopuli

関連する問題