2016-02-19 3 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 

план модель имеет много подписок. Как можно сослаться на указанный план завода с Subscription завода (ниже делает сопли работы):

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> 

Надеется, что это помогает

Смежные вопросы