【问题标题】:FactoryGirl - reference another association factoryFactoryGirl - 引用另一个关联工厂
【发布时间】:2016-02-19 20:13:26
【问题描述】:

我为我的 Plan 模型定义了一些不同的工厂,如下所示:

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

计划模型有许多订阅。如何从订阅工厂引用指定的计划工厂(以下不起作用):

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

有什么想法吗?谢谢。

【问题讨论】:

    标签: factory-bot


    【解决方案1】:

    我自己想通了, - 你必须使用如下特征:

    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" = 'haven@johnstonmoen.com' LIMIT 1
      SQL (0.6ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["email", "haven@johnstonmoen.com"], ["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" = 'roberta@schowalterdubuque.ca' LIMIT 1
      SQL (0.6ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["email", "roberta@schowalterdubuque.ca"], ["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>
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 2012-09-01
      • 1970-01-01
      相关资源
      最近更新 更多