2014-01-09 19 views
0

我真的想圍繞如何用Factory Girl創建複雜的工廠,而且它是不是容易。使用特質來建立工廠的語法是什麼?

我有以下幾點:

  • 認購belongs_to的用戶

  • 認購belongs_to的計劃

我想測試不同Plans。這是我如何設置它:

FactoryGirl.define do 
    factory :plan do 
    trait :copper do 
     name   { "Copper" } 
     amount  { 5 } 
     stripe_id  { "Economy" } 
     listing_limit { 10 } 
     repositories_allowed { 1 } 
    end 
    trait :copper_multi do 
     name   { "Copper Multi" } 
     amount  { 10 } 
     stripe_id  { "Copper_Multi" } 
     listing_limit { 10 } 
     repositories_allowed { 5 } 
    end 
    trait :bronze do 
     name   { "Bronze" } 
     amount  { 5 } 
     stripe_id  { "Basic" } 
     listing_limit { 10 } 
     repositories_allowed { 1 } 
    end 
    trait :bronze_multi do 
     name   { "Bronze Multi" } 
     amount  { 10 } 
     stripe_id  { "Basic_Multi" } 
     listing_limit { 10 } 
     repositories_allowed { 5 } 
    end 
    end 
end 

Subscription廠是:

FactoryGirl.define do 
    factory :subscription do 
    association :user 
    association :plan 
    start_date { Time.now } 
    end_date { 365.days.from_now } 
    end 

end 

當然,這失敗,因爲Plan工廠不能同時沒有指定一個特性來使用。這是設計。

此外,之間有什麼區別:

factory :subscription do 
    association :user 
    end 

和:

factory :subscription do 
    user 
    end 

回答

1

指定默認的計劃

FactoryGirl.define do 
    factory :plan do 
    name   { "Default" } 
    amount  { 1 } 
    stripe_id  { "Default" } 
    listing_limit { 10 } 
    repositories_allowed { 1 } 

    trait :copper do 
     name   { "Copper" } 
     amount  { 5 } 
     stripe_id  { "Economy" } 
     listing_limit { 10 } 
     repositories_allowed { 1 } 
    end 
    end 
end 

現在,這個應該工作

FactoryGirl.define do 
    factory :subscription do 
    association :user 
    association :plan 
    start_date { Time.now } 
    end_date { 365.days.from_now } 
    end 
end 

如果你想創建一個特定性狀(銅)的計劃訂閱

FactoryGirl.define do 
    factory :subscription do 
    association :user 
    association :plan, :factory => [:plan, :copper] 
    start_date { Time.now } 
    end_date { 365.days.from_now } 
    end 
end 

factory :subscription do 
    association :user 
    end 

factory :subscription do 
    user 
end 

但是你可以使用之間沒有什麼區別稍後僅當協會名稱和工廠名稱匹配時

相關問題