Skip to content

Matchers

Marko Justinek edited this page Oct 26, 2020 · 44 revisions

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:)

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

Example:

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

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

// JSON from provider should return an array of `String`, 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.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"
}

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