Pact :: JsonDiffer与pact-messages gem的行为不符合预期

时间:2018-04-27 21:57:48

标签: pact pact-ruby

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

这是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?返回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": ".." }哈希值与预期的字符串值进行比较,而不是将item["contents"]pact-support matchers source code中指示的预期字符串值进行比较,但我不知道为什么或如何解决它。

奇怪的是我在另一个项目中使用了pact-messages合同测试,代码实际上是相同的。我一直在调试和源代码潜入各种Pact repos而没有运气。我很难过。建议?

pact宝石版1.22.2

pact-messages宝石版0.2.0

ruby​​版本2.4

1 个答案:

答案 0 :(得分:1)

该宝石并非由核心契约小组编写。官方Pact Message gem https://github.com/pact-foundation/pact-message-ruby目前处于alpha状态,但很快就会投入生产。