Go 中的 Pact 消费者测试。 dsl.Match 函数的问题

2024年 2月 11日 25.6k 0

go 中的 pact 消费者测试。 dsl.match 函数的问题

php小编苹果今天要和大家分享关于Go中的Pact消费者测试的问题。在使用Pact进行消费者测试时,我们经常会使用dsl.Match函数来匹配请求和响应中的特定字段。然而,我们可能会遇到一些问题,比如无法正确匹配字段的问题。本文将深入探讨这个问题,并提供解决方案,帮助大家更好地应对Pact消费者测试中的挑战。

问题内容

我正在用 go 编写 pact 消费者测试。当我定义交互时,我需要添加预期的响应对象。提供者服务是用 php 编写的,这是我期望的响应:

return [
'status' => 'success',
'data' => [
'configuration' => associative array,
'undeploy_configuration' => associative array,
'meta_data' => associative array,
'undeploy_lora_app_key' => string,
],
];

登录后复制

这是我在 go 中创建的对象,用于表示我应该得到的响应:

deviceconfigurationresponse := dsl.like(map[string]interface{}{
"status": "success",
"data": dsl.like(map[string]interface{}{
"configuration": dsl.mapmatcher{
"config1": dsl.string("value1"),
"config2": dsl.string("value2"),
},
"undeploy_configuration": dsl.mapmatcher{
"undeploy1": dsl.string("value3"),
"undeploy2": dsl.string("value4"),
},
"meta_data": dsl.mapmatcher{
"meta1": dsl.string("info1"),
"meta2": dsl.string("info2"),
},
"undeploy_lora_app_key": dsl.string("example_undeploy_lora_app_key"),
}),
})

登录后复制

但是,当我运行测试时,我收到此错误:

--- fail: testgetdeviceconfiguration (1.79s)
panic: match: unhandled type: interface {} [recovered]
panic: match: unhandled type: interface {}

登录后复制

这是完整的代码:

func TestGetDeviceConfiguration(t *testing.T) {
// Create Pact client
pact := &dsl.Pact{
Consumer: "consumer",
Provider: "provider",
PactDir: "./pacts",
}

defer pact.Teardown()

deviceConfigurationResponse := dsl.Like(map[string]interface{}{
"status": "success",
"data": dsl.Like(map[string]interface{}{
"configuration": dsl.MapMatcher{
"config1": dsl.String("value1"),
"config2": dsl.String("value2"),
},
"undeploy_configuration": dsl.MapMatcher{
"undeploy1": dsl.String("value3"),
"undeploy2": dsl.String("value4"),
},
"meta_data": dsl.MapMatcher{
"meta1": dsl.String("info1"),
"meta2": dsl.String("info2"),
},
"undeploy_lora_app_key": dsl.String("example_undeploy_lora_app_key"),
}),
})

// Define the expected interaction with the provisioning-service
value := "123456789"
pact.
AddInteraction().
Given("Device configuration exists for the given device ID").
UponReceiving("A request to get device configuration").
WithRequest(dsl.Request{
Method: "GET",
Path: dsl.String(fmt.Sprintf("/api/prov/state/%s/configuration", value)),
Headers: dsl.MapMatcher{"Accept": dsl.String("application/json")},
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
Body: dsl.Match(deviceConfigurationResponse),
})

// Test the OnSessionEstablished function
var test = func() error {
cache := new(CacheMock)
deviceConfigGetter := new(DeviceConfigGetterMock)

_, err := GetDeviceConfiguration(value)

cache.AssertExpectations(t)
deviceConfigGetter.AssertExpectations(t)

return err
}

// Verify the interaction with the provider
var err = pact.Verify(test)
assert.NoError(t, err)
}

登录后复制

解决方法

您正在使用的方法(match)采用带有结构标记注释的结构(请参阅https://github.com/pact-foundation/pact-go#auto-generate-matchers-from-struct-tags) 指定如何在结构应该匹配。您已经手动提供了具有正确匹配器的结构,因此根本不需要将其包装在 match 中。

这样的事情应该有效:

WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
Body: deviceConfigurationResponse,
})

登录后复制

以上就是Go 中的 Pact 消费者测试。 dsl.Match 函数的问题的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论