Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Flexible matching

Beth Skurrie edited this page Jan 29, 2015 · 6 revisions

Please read about using regular expressions and type based matching here before continuing.

Note: The following will only work with verifications done by the Ruby Pact library, because it uses a Ruby specific way of serialising the data structure.

Match by regular expression

Define this function (it will be added to Pact soon).

function term(term) {
  return {
    "json_class": "Pact::Term",
    "data": {
      "generate": term.generate,
      "matcher": {
        "json_class": "Regexp",
        "o": 0,
        "s": term.matcher
      }
    }
  };
}

Remember that the mock service is written in Ruby, so the regular expression must be in a Ruby format, not a Javascript format.

provider
  .given('there is a product')
  .uponReceiving("request for products")
  .withRequest({
    method: "get",
    path: "/products"
  })
  .willRespondWith(
    200,
    {},
    {
      "collection": [
        {
          guid: term({matcher: "\\d{16}", generate: "1111222233334444"})
        }
      ]
    }
  );

Match based on type

Define this function (it will be added to Pact soon).

function somethingLike(value) {
  return {
    "json_class": "Pact::SomethingLike",
    "contents" : value
  };
}
provider
  .given('there is a product')
  .uponReceiving("request for products")
  .withRequest({
    method: "get",
    path: "/products"
  })
  .willRespondWith(
    200,
    {},
    {
      "collection": [
        {
          id: somethingLike(1)
        }
      ]
    }
  );
Clone this wiki locally