-
Notifications
You must be signed in to change notification settings - Fork 16
Matchers
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).
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]]};
// Provider should return a `Decimal`.
// The provided value will be used in consumer unit tests:
{
"amount": 123.45
}
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
can either be 0
(zero) or a positive value. 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
.
Setting min: 0
indicates to the provider that the returned set can have zero elements.
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]};
// Provider should return an array of `String`.
// The provided value will be used in consumer unit tests: :
{
"object": ["Teapot", "Teapot"]
}
Defines the exact value (and type) expected to be returned by the provider.
The first value in the set is the value used as an example that MockService will return when running your consumer's unit tests.
Example:
// DSL Swift:
["amount": Matcher.EqualTo(5231)]
// DSL Objective-C:
@{@"amount": [[PFMatcherEqualTo alloc] value:@5231]};
// Provider should return an `Int`.
// The provided value will be used in consumer unit tests:
{
"amount": 5231
}
Available from v0.8.0
.
Defines a matcher that uses Provider State injected value. Provided parameter is the Provider injected element and value is used in the consumer test.
Example:
// DSL:
[
"intId": Matcher.FromProviderState(parameter: "userId", value: .int(666)),
"stringId": Matcher.FromProviderState(parameter: "/api/v1/${stringId}", value: .string("/api/v1/abc-123")),
]
// DSL Objective-C:
@{
@"intId": [[PFMatcherFromProviderState alloc] parameter:@"userId" withIntValue:@666],
@"stringId": [[PFMatcherFromProviderState alloc] parameter:@"/api/v1/${stringId}" withStringValue:@"/api/v1/abc-123"]
};
// Provider should inject the values for parameter as defined in the matcher.
// The provided `value` will be used in consumer unit tests:
{
"intId": 666,
"stringId": "/api/v1/abc-123"
}
Supported types for value
:
.bool(Bool)
.decimal(Decimal)
.double(Double)
.float(Float)
.int(Int)
.string(String)
See https://pactflow.io/blog/injecting-values-from-provider-states/ for more context.
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
// Provider should return a `String` with the provided keywords.
// The provided value will be used in consumer unit tests:
{
"sentence": ["I'm such a big Teapot"]
}
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]};
// Provider should return an `Int`.
// The provided value will be used in consumer unit tests:
{
"amount": 123
}
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]};
// Provider should return a `null`.
// `null` be used in consumer unit tests:
{
"some_nullable_key": null
}
Available from PactSwift
version v0.5.1
.
Defines a matcher that can be used when expecting 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
// Provider should return one of the provided values matching the `Type` **and** value.
// The first provided value will be used for the consumer tests:
{
"some_string_case": "white",
"some_int_case": 5
}
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(value: "2020:01:31", pattern:#"\d{4}:\d{2}:\d{2}"#)]
// DSL Objective-C:
@{@"object": [[PFMatcherRegexLike alloc] value:@"2020:01:31" pattern:@"\\d{4}:\\d{2}:\\d{2}"]};
// Provider should return a value matching regex pattern.
// The provided value will be used in consumer unit tests:
{
"object": "2020:01:31"
}
The Provided value
must match the regex pattern.
Matcher.Regexlike(value: pattern:)
is the only Matcher that can be used to define a Request path in a Swift project.
Example:
.withRequest(
method: .GET,
path: Matcher.RegexLike(value: "/hello/dear/world", pattern: #"^/\w+/([a-z])+/world$"#)
)
Defines the type to be returned by the provider. Use this matcher when declaring simple Codable
types such as String
and Int
.
Provided value is the value used as an example that MockService will return when running your unit tests.
SomethingLike(_:)
does not extract any other matchers or example generators if a nested custom type is passed into it.
Example:
// DSL Swift:
[
"object": Matcher.SomethingLike("Teapot"),
"amount": Matcher.SomethingLike(1)
]
// DSL Objective-C:
@{@"object": [[PFMatcherSomethingLike alloc] value:@"Teapot"], @"amount": [[PFMatcherSomethingLike alloc] value:@1]};
// Provider should return a the `Type` of provided value (`Type` must conform to `Encodable` protocol).
// The provided value will be used in consumer unit tests:
{
"object": "Teapot",
"amount": 1
}