首页 > 解决方案 > Pact::JsonDiffer 与 pact-messages gem 的行为不符

问题描述

我正在使用pact-messages gem 为事件消息队列提供程序的使用者生成一个 Pact 文件。

这是契约文件:

{
  "consumer": {
    "name": "Ice Cream"
  },
  "provider": {
    "name": "Desserts"
  },
  "interactions": [{
    "description": null,
    "providerState": "dessert_created",
    "request": {
      "method": "MESSAGE",
      "path": "/"
    },
    "response": {
      "body": {
        "attributes": {
          "event_name": "dessert_created",
          "event_time": {
            "json_class": "Pact::SomethingLike",
            "contents": "2018-03-05T21:00:32.321Z"
          }
        },
        "body": {
          "dessert_resource_link_id": {
            "json_class": "Pact::SomethingLike",
            "contents": "407245344ee3f65fe3a8b11d77ca9b11ea4660e8"
          },
          "dessert_resource_link_id_duplicated_from": {
            "json_class": "Pact::SomethingLike",
            "contents": "407245344ee3f65fe3a8b11d77ca9b11ea4660e8"
          }
        }
      }
    }
  }],
  "metadata": {
    "pactSpecification": {
      "version": "1.0.0"
    }
  }
}

这是event_message提供者生成的(在 Ruby 中):

{
  "attributes" => {
    "event_name" => "dessert_created", "event_time" => "2018-04-27T21:09:06Z"
  }, "body" => {
    "dessert_id" => "21", "context_id" => "11", "context_type" => "Ice Cream", "workflow_state" => "duplicating", "title" => "Rocky Road", "description" => "chocolate with almonds and marshmallows", "updated_at" => "2018-04-27T21:09:06Z", "dessert_resource_link_id" => "769151f4f462812b8c630f3dfd8af84567a29a03", "dessert_resource_link_id_duplicated_from" => "769151f4f462812b8c630f3dfd8af84567a29a03"
  }
}

这是我将 Pact 与生成的消息进行比较的代码:

      def has_kept_the_contract?
        diff = compare_contract_with_live_event
        contract_matches = diff.none?
        print_difference(diff) unless contract_matches
        contract_matches
      end

      private

      def compare_contract_with_live_event
        Pact::JsonDiffer.call(contract_message, event_message)
      end

      def contract_message
        Pact::Messages.get_message_contract(
          'Desserts',
          'Ice Cream',
          'dessert_created'
        )
      end

      def print_difference(diff)
        puts Pact::Matchers::UnixDiffFormatter.call(diff)
      end

我希望has_kept_the_contract?return true,但合同失败,并且该print_difference方法从 Pact 输出以下内容:

Diff
--------------------------------------
Key: - is expected
     + is actual
Matching keys and values are not shown

 {
   "attributes": {
-    "event_time": {
-      "json_class": "Pact::SomethingLike",
-      "contents": "2018-03-05T21:00:32.321Z"
-    }
+    "event_time": "2018-04-27T20:58:44Z"
   },
   "body": {
-    "dessert_resource_link_id": {
-      "json_class": "Pact::SomethingLike",
-      "contents": "407245344ee3f65fe3a8b11d77ca9b11ea4660e8"
-    },
-    "dessert_resource_link_id_duplicated_from": {
-      "json_class": "Pact::SomethingLike",
-      "contents": "407245344ee3f65fe3a8b11d77ca9b11ea4660e8"
-    }
+    "dessert_resource_link_id": "67ad1125cec845b128dac090565dc4e59d687df3",
+    "dessert_resource_link_id_duplicated_from": "67ad1125cec845b128dac090565dc4e59d687df3"
   }
 }

Description of differences
--------------------------------------
* Expected a Hash but got a String ("2018-04-27T20:58:44Z") at $.attributes.event_time
* Expected a Hash but got a String ("67ad1125cec845b128dac090565dc4e59d687df3") at $.body.dessert_resource_link_id
* Expected a Hash but got a String ("67ad1125cec845b128dac090565dc4e59d687df3") at $.body.dessert_resource_link_id_duplicated_from

似乎 Pact 正在将完整的"item": { "json_class": "..", "contents": ".." }哈希值与预期的 String 值进行比较,而不是与pact-support matchers source codeitem["contents"]中指示的预期 String 值进行比较,但我不知道为什么或如何修复它。

奇怪的是,我在不同的项目中进行了 pact-messages 合同测试,并且代码几乎相同。我一直在调试和源代码潜入各种 Pact 存储库,但没有运气。我难住了。建议?

pact宝石版1.22.2

pact-messages宝石版0.2.0

红宝石版本2.4

标签: pactpact-ruby

解决方案


那颗宝石不是由核心 Pact 团队编写的。官方的 Pact Message gem https://github.com/pact-foundation/pact-message-ruby目前处于 alpha 阶段,但应该很快就能投入生产。


推荐阅读