Search code examples
pact

Pact verify provider, what does Pact::UnexpectedIndex mean?


I am using Pact for Consumer Driven Contracts testing. In my usecase, my consumer "some-market-service-consumer" is using provider "market-service". The contract "produced" at some-market-service-consumer, looks like this:

{
"provider": {
    "name": "market-service"
},
"consumer": {
    "name": "some-market-service-consumer"
},
"interactions": [
    {
        "description": "Get all markets",
        "request": {
            "method": "GET",
            "path": "/markets"
        },
        "response": {
            "status": 200,
            "headers": {
                "Content-Type": "application/json; charset=utf-8"
            },
            "body": {
                "markets": [
                    {
                        "phone": "HBiQOAeQegaWtbcHfAro"
                    }
                ]
            },
            "matchingRules": {
                "$.headers.Content-Type": {
                    "regex": "application/json; charset=utf-8"
                },
                "$.body.markets[*].phone": {
                    "match": "type"
                },
                "$.body.markets": {
                    "min": 1,
                    "match": "type"
                }
            }
        }
    }
],
"metadata": {
    "pact-specification": {
        "version": "2.0.0"
    },
    "pact-jvm": {
        "version": "3.3.6"
    }
}

}

On provider-site, I am using pact-provider-verifier-docker¹. Here is my test-result:

    WARN: Ignoring unsupported matching rules {"min"=>1} for path $['body']['markets']
    .....
       @@ -1,7 +1,7 @@
    {
      "markets": [
        ... ,
   -    Pact::UnexpectedIndex,
   +    Hash,
      ]
    }

   Key: - means "expected, but was not found". 
        + means "actual, should not be found". 
        Values where the expected matches the actual are not shown.

It seems, as if the testing works fine - "phone" is tested valid. But at this point, I have no clue, what (expected) "Pact::UnexpectedIndex" means, and why it fails. Where does this expectation come from, how to fix it?

¹ In special, my own version, which uses most recent external dependencies.


Solution

  • As you can see in this test case here the Pact::UnexpectedIndex is used to indicate than an array is longer than was expected. I think what it's trying to say is that there is an extra hash at the end of the array. (I agree that it is not clear at all! I wrote this code, so I apologise for its confusing nature. It turns out that the hardest part of writing pact code was writing helpful diff output.)

    What is confusing about that error is that it should be allowing you to have extra elements as you've specified $.body.markets to have a minimum length of 1. Perhaps the docker verifier is using version 1 matching instead of version 2?