{"id":207992,"date":"2025-07-08T13:14:40","date_gmt":"2025-07-08T05:14:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207992\/"},"modified":"2025-07-08T13:14:40","modified_gmt":"2025-07-08T05:14:40","slug":"go-%e4%b8%ad%e7%9a%84-pact-%e6%b6%88%e8%b4%b9%e8%80%85%e6%b5%8b%e8%af%95%e3%80%82-dsl-match-%e5%87%bd%e6%95%b0%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207992\/","title":{"rendered":"Go \u4e2d\u7684 Pact \u6d88\u8d39\u8005\u6d4b\u8bd5\u3002 dsl.Match \u51fd\u6570\u7684\u95ee\u9898"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Go \u4e2d\u7684 Pact \u6d88\u8d39\u8005\u6d4b\u8bd5\u3002 dsl.Match \u51fd\u6570\u7684\u95ee\u9898<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 17:36:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u4e2d\u7684 Pact \u6d88\u8d39\u8005\u6d4b\u8bd5\u3002 dsl.Match \u51fd\u6570\u7684\u95ee\u9898\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u7528 go \u7f16\u5199 pact \u6d88\u8d39\u8005\u6d4b\u8bd5\u3002\u5f53\u6211\u5b9a\u4e49\u4ea4\u4e92\u65f6\uff0c\u6211\u9700\u8981\u6dfb\u52a0\u9884\u671f\u7684\u54cd\u5e94\u5bf9\u8c61\u3002\u63d0\u4f9b\u8005\u670d\u52a1\u662f\u7528 php \u7f16\u5199\u7684\uff0c\u8fd9\u662f\u6211\u671f\u671b\u7684\u54cd\u5e94\uff1a<\/p>\n<pre>return  [\n            'status' =&gt; 'success',\n            'data' =&gt; [\n                'configuration' =&gt; associative array,\n                'undeploy_configuration' =&gt; associative array,\n                'meta_data' =&gt; associative array,\n                'undeploy_lora_app_key' =&gt; string,\n            ],\n        ];\n<\/pre>\n<p>\u8fd9\u662f\u6211\u5728 go \u4e2d\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u8868\u793a\u6211\u5e94\u8be5\u5f97\u5230\u7684\u54cd\u5e94\uff1a<\/p>\n<pre>deviceconfigurationresponse := dsl.like(map[string]interface{}{\n        \"status\": \"success\",\n        \"data\": dsl.like(map[string]interface{}{\n            \"configuration\": dsl.mapmatcher{\n                \"config1\": dsl.string(\"value1\"),\n                \"config2\": dsl.string(\"value2\"),\n            },\n            \"undeploy_configuration\": dsl.mapmatcher{\n                \"undeploy1\": dsl.string(\"value3\"),\n                \"undeploy2\": dsl.string(\"value4\"),\n            },\n            \"meta_data\": dsl.mapmatcher{\n                \"meta1\": dsl.string(\"info1\"),\n                \"meta2\": dsl.string(\"info2\"),\n            },\n            \"undeploy_lora_app_key\": dsl.string(\"example_undeploy_lora_app_key\"),\n        }),\n    })\n<\/pre>\n<p>\u4f46\u662f\uff0c\u5f53\u6211\u8fd0\u884c\u6d4b\u8bd5\u65f6\uff0c\u6211\u6536\u5230\u6b64\u9519\u8bef\uff1a<\/p>\n<pre>--- fail: testgetdeviceconfiguration (1.79s)\npanic: match: unhandled type: interface {} [recovered]\n        panic: match: unhandled type: interface {}\n\n<\/pre>\n<p>\u8fd9\u662f\u5b8c\u6574\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>func TestGetDeviceConfiguration(t *testing.T) {\n    \/\/ Create Pact client\n    pact := &amp;dsl.Pact{\n        Consumer: \"consumer\",\n        Provider: \"provider\",\n        PactDir:  \".\/pacts\",\n    }\n\n    defer pact.Teardown()\n\n    deviceConfigurationResponse := dsl.Like(map[string]interface{}{\n        \"status\": \"success\",\n        \"data\": dsl.Like(map[string]interface{}{\n            \"configuration\": dsl.MapMatcher{\n                \"config1\": dsl.String(\"value1\"),\n                \"config2\": dsl.String(\"value2\"),\n            },\n            \"undeploy_configuration\": dsl.MapMatcher{\n                \"undeploy1\": dsl.String(\"value3\"),\n                \"undeploy2\": dsl.String(\"value4\"),\n            },\n            \"meta_data\": dsl.MapMatcher{\n                \"meta1\": dsl.String(\"info1\"),\n                \"meta2\": dsl.String(\"info2\"),\n            },\n            \"undeploy_lora_app_key\": dsl.String(\"example_undeploy_lora_app_key\"),\n        }),\n    })\n\n    \/\/ Define the expected interaction with the provisioning-service\n    value := \"123456789\"\n    pact.\n        AddInteraction().\n        Given(\"Device configuration exists for the given device ID\").\n        UponReceiving(\"A request to get device configuration\").\n        WithRequest(dsl.Request{\n            Method:  \"GET\",\n            Path:    dsl.String(fmt.Sprintf(\"\/api\/prov\/state\/%s\/configuration\", value)),\n            Headers: dsl.MapMatcher{\"Accept\": dsl.String(\"application\/json\")},\n        }).\n        WillRespondWith(dsl.Response{\n            Status:  200,\n            Headers: dsl.MapMatcher{\"Content-Type\": dsl.String(\"application\/json\")},\n            Body:    dsl.Match(deviceConfigurationResponse),\n        })\n\n    \/\/ Test the OnSessionEstablished function\n    var test = func() error {\n        cache := new(CacheMock)\n        deviceConfigGetter := new(DeviceConfigGetterMock)\n\n        _, err := GetDeviceConfiguration(value)\n\n        cache.AssertExpectations(t)\n        deviceConfigGetter.AssertExpectations(t)\n\n        return err\n    }\n\n    \/\/ Verify the interaction with the provider\n    var err = pact.Verify(test)\n    assert.NoError(t, err)\n}\n<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u6b63\u5728\u4f7f\u7528\u7684\u65b9\u6cd5\uff08<code>match<\/code>\uff09\u91c7\u7528\u5e26\u6709\u7ed3\u6784\u6807\u8bb0\u6ce8\u91ca\u7684\u7ed3\u6784\uff08\u8bf7\u53c2\u9605) \u6307\u5b9a\u5982\u4f55\u5728\u7ed3\u6784\u5e94\u8be5\u5339\u914d\u3002\u60a8\u5df2\u7ecf\u624b\u52a8\u63d0\u4f9b\u4e86\u5177\u6709\u6b63\u786e\u5339\u914d\u5668\u7684\u7ed3\u6784\uff0c\u56e0\u6b64\u6839\u672c\u4e0d\u9700\u8981\u5c06\u5176\u5305\u88c5\u5728 <code>match<\/code> \u4e2d\u3002<\/p>\n<p>\u8fd9\u6837\u7684\u4e8b\u60c5\u5e94\u8be5\u6709\u6548\uff1a<\/p>\n<pre>        WillRespondWith(dsl.Response{\n            Status:  200,\n            Headers: dsl.MapMatcher{\"Content-Type\": dsl.String(\"application\/json\")},\n            Body:    deviceConfigurationResponse,\n        })\n<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aGo \u4e2d\u7684 Pact \u6d88\u8d39\u8005\u6d4b\u8bd5\u3002 dsl.Match \u51fd\u6570\u7684\u95ee\u9898\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207992","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207992"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207992\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}