2011-06-29 67 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