2016-02-19 60 views
0

我已經如下定義了我的計劃模型的一些不同的工廠:FactoryGirl - 引用另一個關聯工廠

FactoryGirl.define do 
    factory :plan do 
    factory :free_plan do 
     name "Free" 
     description "Free Plan" 
     price 0 
     duration 999 
     questions 5 
    end 

    factory :premium_plan do 
     name "Premium" 
     description "Premium Plan" 
     duration 1 
     price 3 
     questions 1000 
    end 

    factory :premium_plus_plan do 
     name "Premium Plus" 
     description "Premium Plus Plan" 
     duration 3 
     price 7 
     questions 1000 
    end 
    end 
end 

規劃模型有很多訂閱。如何從訂閱工廠引用指定的計劃工廠(以下是snot工作):

FactoryGirl.define do 
    factory :subscription do 
    user 
    activated true 

    factory :one_month_subscription do 
     plan { premium_plan } 
     start_date { Time.now } 
     end_date { start_date.advance(months: 1)} 
    end 

    factory :three_months_subscription do 
     plan { premium_plus_plan } 
     start_date { Time.now } 
     end_date { start_date.advance(months: 3)} 
    end 

    factory :expired_subscription do 
     plan { premium_plus_plan } 
     start_date { 2.years.ago } 
     end_date { start_date.advance(year: 1) } 
    end 
    end 
end 

任何想法?謝謝。

回答

0

想通了我自己, - 你將有如下使用特點:

FactoryGirl.define do 
    factory :plan do 
    trait :free_plan do 
     name "Free" 
     description "Free Plan" 
     price 0 
     duration 999 
     questions 20 
    end 

    trait :premium_plan do 
     name "Premium" 
     description "Premium Plan" 
     duration 1 
     price 3 
     questions 1000 
    end 

    trait :premium_plus_plan do 
     name "Premium Plus" 
     description "Premium Plus Plan" 
     duration 3 
     price 7 
     questions 1000 
    end 
    end 
end 

然後打電話給他們在協會這樣的:

FactoryGirl.define do 
    factory :subscription do 
    user 
    activated true 

    factory :one_month_subscription do 
     association :plan, :premium_plan 
     start_date { Time.now } 
     end_date { start_date.advance(months: 1)} 
    end 

    factory :three_months_subscription do 
     association :plan, :premium_plus_plan 
     start_date { Time.now } 
     end_date { start_date.advance(months: 3)} 
    end 

    factory :expired_subscription do 
     association :plan, :premium_plus_plan 
     start_date { 2.years.ago } 
     end_date { start_date.advance(year: 1) } 
    end 
    end 
end 

現在您可以輕鬆創建任何訂閱廠爲(例如在Rails控制檯中):

rails c --sandbox 

[2] pry(main)> FactoryGirl.build(:one_month_subscription) 
    (0.5ms) SAVEPOINT active_record_1 
    User Exists (2.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    SQL (0.6ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "[email protected]"], ["encrypted_password", "$2a$10$Kt27fqceEJijx11WCdQWHOp8g2DJfAjIqhEi9B82KF.4AeU7536JW"], ["created_at", "2016-02-19 12:06:39.875910"], ["updated_at", "2016-02-19 12:06:39.875910"]] 
    (0.3ms) RELEASE SAVEPOINT active_record_1 
    (0.5ms) SAVEPOINT active_record_1 
    Plan Exists (1.1ms) SELECT 1 AS one FROM "plans" WHERE LOWER("plans"."name") = LOWER('Premium') LIMIT 1 
    SQL (0.9ms) INSERT INTO "plans" ("name", "description", "duration", "price", "questions", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Premium"], ["description", "Premium Plan"], ["duration", 1], ["price", "3.0"], ["questions", 1000], ["created_at", "2016-02-19 12:06:39.912615"], ["updated_at", "2016-02-19 12:06:39.912615"]] 
    (0.4ms) RELEASE SAVEPOINT active_record_1 
=> #<Subscription:0x007f9da94ae360 
id: nil, 
user_id: 20, 
start_date: Fri, 19 Feb 2016, 
end_date: Sat, 19 Mar 2016, 
activated: true, 
created_at: nil, 
updated_at: nil, 
express_token: nil, 
express_payer_id: nil, 
plan_id: 3> 
[3] pry(main)> FactoryGirl.build(:three_months_subscription) 
    (0.5ms) SAVEPOINT active_record_1 
    User Exists (0.7ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    SQL (0.6ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "[email protected]"], ["encrypted_password", "$2a$10$wsyEzEvm78IF.FLkRaOcPOyyaAx0Fi5eIWU4IyY/ENpzLBrqGXhRS"], ["created_at", "2016-02-19 12:07:24.774203"], ["updated_at", "2016-02-19 12:07:24.774203"]] 
    (0.3ms) RELEASE SAVEPOINT active_record_1 
    (0.3ms) SAVEPOINT active_record_1 
    Plan Exists (0.7ms) SELECT 1 AS one FROM "plans" WHERE LOWER("plans"."name") = LOWER('Premium Plus') LIMIT 1 
    SQL (0.5ms) INSERT INTO "plans" ("name", "description", "duration", "price", "questions", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Premium Plus"], ["description", "Premium Plus Plan"], ["duration", 3], ["price", "7.0"], ["questions", 1000], ["created_at", "2016-02-19 12:07:24.780799"], ["updated_at", "2016-02-19 12:07:24.780799"]] 
    (0.3ms) RELEASE SAVEPOINT active_record_1 
=> #<Subscription:0x007f9da93ec9e0 
id: nil, 
user_id: 21, 
start_date: Fri, 19 Feb 2016, 
end_date: Thu, 19 May 2016, 
activated: true, 
created_at: nil, 
updated_at: nil, 
express_token: nil, 
express_payer_id: nil, 
plan_id: 4> 

希望這有助於

相關問題