0

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.

Markus Schulte
  • 4,171
  • 3
  • 47
  • 58

1 Answers1

3

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?

Beth Skurrie
  • 1,333
  • 7
  • 8
  • Could make sense, as I am getting "WARN: Ignoring unsupported matching rules {"min"=>1} for path $['body']['markets']" on the run. Thanks for your feedback. I will have a look soon. – Markus Schulte Mar 24 '17 at 09:25
  • Something about the rules is not right, because it should work. See this method here is trying to find the min: https://github.com/bethesque/pact-support/blob/d27def23b778503b7f3104ea87f9eb5a406d4801/lib/pact/matching_rules/merge.rb#L54 – Beth Skurrie Mar 25 '17 at 23:27
  • The pact is (syntactically) valid, isn't it? – Markus Schulte Mar 26 '17 at 10:58
  • I believe so. It's just occurred to me that perhaps the min feature isn't implemented yet. The v2 matching hasn't been finished in the Ruby implementation (mostly because I've been away on mat leave for a year) and I suspect this part hasn't been done. – Beth Skurrie Mar 28 '17 at 05:01
  • Nope, it's [implemented](https://github.com/bethesque/pact-support/blob/b4acb1cf2af9a1fefdc1e6a44df33f68e707938c/lib/pact/matchers/matchers.rb#L81). – Beth Skurrie Mar 28 '17 at 06:47
  • Can you post the actual response, and I'll see if I can do some debugging. – Beth Skurrie Mar 28 '17 at 06:48
  • First of all, I marked this answer as correct one, as it did answer my question. And with this knowledge, I was able to workaround my problem, by increasing numberOfExamples in the Pact. Right now I am missing time for reporting this issue, but I wont forget to give feedback (maybe a bug-issue at https://github.com/pact-foundation/pact-provider-verifier). – Markus Schulte Apr 05 '17 at 14:47
  • I've added an [issue](https://github.com/bethesque/pact-support/issues/31) in pact-support to improve the error message. Having just gone through the scenario, I'd be interested in hearing your thoughts on what would have made more sense to you, so please comment on the issue if you have suggestions. – Beth Skurrie Apr 06 '17 at 23:09