Skip to content

Matchers

Marko Justinek edited this page Jul 12, 2021 · 44 revisions

You can use Matchers when you are defining your Request query, Request headers, Request body, Response headers and Response Body. Request path only accepts Matcher.RegexLike(_:).

More information about Pact Matching concepts can be found at https://docs.pact.io/getting_started/matching/.

Note: In Objective-C, the Matchers are prefixed with PFMatcher (PF - Pact Foundation).

Matcher.DecimalLike(_:)

Defines the value expected to be returned from the server as a Decimal.
Provided value is the value used as an example that MockService will return when running your unit tests.

Example:

// DSL Swift: 
["amount": Matcher.DecimalLike(123.45)]

// DSL Objective-C:
@{@"amount": [[PFMatcherDecimalLike alloc] value:[@123.45 decimalValue]]};

// JSON from the provider should return a `Decimal`, MockServer will return: 
{ 
  "amount": 123.45
}

Matcher.EachLike(_:min:max:count:)

Defines the value expected to be returned by the provider as an Array of type defined in your expectation declaration.
Provided value is the template for MockService to return when running your unit tests.

count argument defines how many elements MockServer will generate in its response. min, max and count need to be a positive value where count must be equal or greater than min, and equal or less than max. Given provided min is greater than max then the lesser value will be used for min and greater value for max.

Example:

// DSL Swift: 
["object": Matcher.EachLike("Teapot", min: 2)]
["object": Matcher.EachLike("Teapot", min: 1, count: 2)]
["object": Matcher.EachLike("Teapot", max: 5, count: 2)]

// DSL Objective-C:
@{@"object": [[PFMatcherEachLike alloc] value:@"Teapot" min:@2 max:@10 count:@5]};

// JSON from provider should return an array of `String` with two , MockServer will return: 
{ 
  "object": ["Teapot", "Teapot"]
}

Matcher.EqualTo(_:)

Defines the exact value (and type) expected to be returned by the provider.
Provided value is the value used as an example that MockService will return when running your unit tests.

Example:

// DSL Swift: 
["amount": Matcher.EqualTo(5231)]

// DSL Objective-C:
@{@"amount": [[PFMatcherEqualTo alloc] value:@5231]};

// JSON from provider should return `Int` 5231, MockServer will return: 
{ 
  "amount": 5231
}

Matcher.IncludesLike(_:)

Defines a Pact matcher that expects a set of values included in the returned String. Provided value is the value used as an example that MockService will return when running your unit tests.

Example:

// DSL: 
["sentence": Matcher.IncludesLike("I'm", "a", "Teapot", combine: .OR, generate: "I'm such a big Teapot")]

// DSL Objective-C:
@{@"sentence": [[PFMatcherIncludesLike alloc] includesAll:@[@"I'm", @"a", @"Teapot", nil] generate:@"I'm such a big Teapot"]}; // for .AND

@{@"sentence": [[PFMatcherIncludesLike alloc] includesAny:@[@"I'm", @"a", @"Teapot", nil] generate:@"I'm such a big Teapot"]}; // for .OR

// JSON from provider should return a `String` with the provided keywords, MockServer will return: 
{ 
  "sentence": ["I'm such a big Teapot"]
}

Matcher.IntegerLike(_:)

Defines the value to be returned by the provider is of type Int. Provided value is the value used as an example and that MockService will return when running your unit tests.

Example:

// DSL Swift: 
["amount": Matcher.IntegerLike(123)]

// DSL Objective-C:
@{@"amount": [[PFMatcherIntegerLike alloc] value:@123]};

// JSON from provider should return an `Int`, MockServer will return: 
{ 
  "amount": 123
}

Matcher.MatchNull()

Defines the expected value for the given key to be null.

Example:

// DSL Swift: 
["some_nullable_key": Matcher.MatchNull()]

// DSL Objective-C: 
@{@"some_nullable_key": [[PFMatcherNull alloc] init]};

// JSON from provider should return a null value for given key: 
{ 
  "some_nullable_key": null
}

Matcher.OneOf(_...)

Available from PactSwift version v0.5.1.

Defines a matcher that can be used when defining a specific list of values (eg: defining an enum). It is a wrapper around the regex matcher.

Initialiser accepts a variadic argument of types String, Int, Decimal and Float.

Note: the first provided value will be used as the example in the test.

Example:

// DSL Swift: 
[
  "some_string_case": Matcher.OneOf("white", "black", "yellow", "blue", "red"),
  "some_int_case": Matcher.OneOf(5, 1, 2, 3, 4),
]


// DSL Objective-C: 
// not implemented

// The first provided value will be used for the consumer tests: 
{ 
  "some_string_case": "white",
  "some_int_case": 5
}

Matcher.RegexLike(_:)

Defines the string conforming to the regex term to be returned by the provider for the given key. Provided value is the value used as an example that MockService will return when running your unit tests.

Example:

// DSL Swift: 
["object": Matcher.RegexLike("2020:01:31", term:#"\d{4}:\d{2}:\d{2}"#)]

// DSL Objective-C:
@{@"object": [[PFMatcherRegexLike alloc] value:@"2020:01:31" term:@"\\d{4}:\\d{2}:\\d{2}"]};

// JSON from provider should return a `String` and an `Int`, MockServer will return: 
{ 
  "object": "2020:01:31"
}

The Provided value must match the regex pattern.
Matcher.Regexlike(_:) is the only Matcher that can be used to define a Request path in a Swift project.

Example:

 .withRequest(
   method: .GET,
   path: Matcher.RegexLike("/hello/dear/world", term: #"^/\w+/([a-z])+/world$"#)
 )

Matcher.SomethingLike(_:)

Defines the type to be returned by the provider. Provided value is the value used as an example that MockService will return when running your unit tests.

Example:

// DSL Swift: 
[
  "object": Matcher.SomethingLike("Teapot"),
  "amount": Matcher.SomethingLike(1)
]

// DSL Objective-C:
@{@"object": [[PFMatcherSomethingLike alloc] value:@"Teapot"], @"amount": [[PFMatcherSomethingLike alloc] value:@1]};

// JSON from provider should return a `String` and an `Int`, MockServer will return: 
{ 
  "object": "Teapot",
  "amount": 1
}

Clone this wiki locally