【问题标题】:Factory_bot_rails create hash with traitFactory_bot_rails 创建具有特征的哈希
【发布时间】:2020-08-06 16:24:09
【问题描述】:

我想使用factory_bot_rails创建如下所示的哈希

let(:expected_hash) do
  {
    'fields' => {
      'summary' => 'member added as a collaborator to repository',
      'description' => "[OutsideCollaboratorAdded]\n-----------------\nMember: member,\n Added to project: repository,\n by: sender",
      'project' => { 'id' => '22501' },
      'issuetype' => { 'id' => '11800' },
      'customfield_14400' => '2020-04-23'
    }
  }
end

我们的想法是让这些字段:summarydescriptionprojectissue_type 可以使用 trait 进行编辑。所以我试图这样做:

  factory :expected_ticket_fields do
    fields {{
      summary => 'test',
      description => 'test desc',
      customfield_14400 => '2020-04-23'
      }}

    trait :member_added do
      summary { 'member added as a collaborator to repository' }
      description { '[OutsideCollaboratorAdded]\n-----------------\nMember: member,\n Added to project: repository,\n by: sender' }
      project {{ 'id' => '22501' }}
    end
  end

在规格方面:

  before do
    hash = create(:expected_ticket_fields)
    hash.member_added
  end

但我收到一个错误:uninitialized constant ExpectedTicketFields

【问题讨论】:

  • 您必须使用 OpenStuct 作为工厂的类。 factory :expected_ticket_fields, class: 'OpenStruct' do ...。但恕我直言,这看起来应该由模型处理。
  • 哈希的目的是什么?正如 max 所说,它看起来应该是一个模型而不是一个哈希。
  • 伙计们,我想通过 factory_bot 创建一个哈希,所以我猜其余的无关紧要。如果您真的想知道它会创建 Jira 票证,并且它是更大级别的一部分。

标签: ruby-on-rails ruby rspec factory-bot


【解决方案1】:

FactoryBot 的工作方式与您使用的不同。

1) FactoryBot 不是 Hash,它是FactoryBot 对象的一种。 2) FactoryBot 需要存在同名的 ActiveRecord(如“Ticket”或“Project”)才能工作。

你想创建一个特征,然后如果你想要那种票,你可以这样写:

factory :ticket do
  # you can specify the default `summary`
  summary { 'This is a ticket' }
  trait :ticket_with_member_added do
    summary { 'This is a ticket with a member added' }
    description { '[OutsideCollaboratorAdded]\n-----------------\nMember: member,\n Added to project: repository,\n by: sender' }
    # If you are planning on using a Project ActiveRecord, then you must ALSO create a factory for project, then this will trigger a Project to be created.
    project
  end
end

# In your tests
ticket = create(:ticket_with_member_added)

【讨论】:

    【解决方案2】:

    试试这个:

    FactoryBot.define do
      factory :apple_store_webhook, class: Hash do
        product_id { "test_recurring_2" }
        notification_type { "DID_RENEW" }
        subtype { "VOLUNTARY" }
        expires_date { 1.month.from_now.in_time_zone.to_i * 1000 }
        purchase_date { Time.zone.now.to_i * 1000 }
        original_purchase_date { Time.zone.now.to_i * 1000 }
        signed_date { Time.zone.now.to_i * 1000 }
    
        initialize_with do
          {
            "transactionId" => "1000000899870910",
            "originalTransactionId" => "1000000883101707",
            "webOrderLineItemId" => "1000000067144880",
            "bundleId" => "com.some.bundleId",
            "productId" => product_id,
            "subscriptionGroupIdentifier" => "20518733",
            "purchaseDate" => purchase_date,
            "originalPurchaseDate" => original_purchase_date,
            "expiresDate" => expires_date,
            "quantity" => 1,
            "type" => "Auto-Renewable Subscription",
            "inAppOwnershipType" => "PURCHASED",
            "signedDate" => signed_date,
            "notificationType" => notification_type,
            "subtype" => subtype,
            "notificationUUID" => "65d80713-d686-41ba-b587-61e0f39d28d9"
          }
        end
    
        trait :refund_webhook do
          after(:build) do |hsh|
            hsh["revocationDate"] = Time.zone.now.to_i * 1000
          end    
        end
      end
    end
    

    应该像这样工作:

    [13] pry(main)> FactoryBot.build :apple_store_webhook, :refund_webhook, notification_type: "FOO"
    => {
                      "transactionId" => "1000000899870910",
              "originalTransactionId" => "1000000883101707",
                 "webOrderLineItemId" => "1000000067144880",
                           "bundleId" => "com.some.bundleId",
                          "productId" => "test_recurring_2",
        "subscriptionGroupIdentifier" => "20518733",
                       "purchaseDate" => 1637651271000,
               "originalPurchaseDate" => 1637651271000,
                        "expiresDate" => 1640243271000,
                           "quantity" => 1,
                               "type" => "Auto-Renewable Subscription",
                 "inAppOwnershipType" => "PURCHASED",
                         "signedDate" => 1637651271000,
                   "notificationType" => "FOO",
                            "subtype" => "VOLUNTARY",
                   "notificationUUID" => "65d80713-d686-41ba-b587-61e0f39d28d9",
                     "revocationDate" => 1637651271000
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2014-04-08
      • 2013-12-20
      • 1970-01-01
      • 2015-07-21
      • 2022-06-14
      • 2016-08-17
      相关资源
      最近更新 更多