契约:验证数组包含匹配的哈希 [英] pact: validate that an array contains a matching hash
问题描述
我有一个返回哈希数组的服务,其顺序是不确定的.我需要验证是否存在一个具有特定键/值的哈希,并且该哈希填充有数据,但是其余的哈希我不在乎它们的数据.例如,如果服务返回以下内容:
I have a service that returns an array of hashes, the order of which is non-deterministic. I need to validate that there exists one hash that has a certain key/value, and that hash is populated with data, but the rest of the hashes I don't care about their data. For example, if the service returns this:
[
{
"key":"meaningless1",
"data": {
}
},
{
"key":"meaningless2",
"data": {
"some": "data",
"goes": ["here"]
}
},
{
"key":"meaningful",
"data": {
"regex": "value",
"integer": 1,
"boolean": true
}
}
]
我想验证数组中是否存在一个具有"key":"meaningful"
的哈希,并且该哈希中的data
哈希具有一个regex
密钥,其值与一个正则表达式匹配,该密钥为integer
是整数,而boolean
的键是布尔值.我不在乎其他散列中是否有数据,或者它们中的数据是否与此模式匹配.
I want to validate that in the array, there is a hash that has "key":"meaningful"
, and that the data
hash in that hash has a key of regex
with a value that matches a regex, a key of integer
that is an integer, and a key of boolean
that is a boolean. I don't care if the other hashes have data at all, or if the data they have matches this schema.
我不能使用EachLike
,因为这将针对所有哈希验证架构,而不仅仅是具有正确的key
的哈希.我也尝试过这样的事情:
I can't use EachLike
, because that will verify the schema against all the hashes, not just the one with the right key
. I also tried something like this:
expected = [
{
"key":"meaningful",
"data":{
"regex":Term("v.*", "value"),
"integer":Like(1),
"boolean":Like(True)
}
}
]
但是它只是尝试针对结果数组中的第0个元素进行验证,这意味着它试图针对以下内容进行验证:
But it simply tried to verify that against the 0th element in the resulting array, which means it tried to verify against:
{
"key":"meaningless1",
"data": {
}
},
我想得到什么吗?
推荐答案
当前无法实现.您可能想加入有关您需要的功能的对话: https://github.com/pact-foundation/pact-specification/issues/38
This is not currently possible. You might like to join this conversation about the feature that you are asking for: https://github.com/pact-foundation/pact-specification/issues/38
但是,我要指出的一件事是,当您可以控制提供程序上的数据时,协定最有效.如果无法控制数据,则契约可能不是适用于您的方案的最佳工具.阅读以下内容:
However, one thing that I will point out is that pact works best when you can control the data on the provider. If you cannot control the data, then pact might not be the best tool for your scenario. Have a read of: https://github.com/pact-foundation/pact-ruby/wiki/Why-Pact-may-not-be-the-best-tool-for-testing-public-APIs
这篇关于契约:验证数组包含匹配的哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!