pact: validate that an array contains a matching hash

ε祈祈猫儿з 提交于 2019-12-11 15:46:10

问题


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
        }
    }
]

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.

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)
        }
    }
]

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": {

    }
},

Is what I want possible?


回答1:


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



来源:https://stackoverflow.com/questions/47635547/pact-validate-that-an-array-contains-a-matching-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!