Skip to content

Instantly share code, notes, and snippets.

@surpher
Last active October 5, 2021 07:04
Show Gist options
  • Save surpher/f98c0da798a286226a7abd557efe7c95 to your computer and use it in GitHub Desktop.
Save surpher/f98c0da798a286226a7abd557efe7c95 to your computer and use it in GitHub Desktop.
Example PactSwift test verifying multiple interactions in one .run{ }
func testExample_AnimalsWithChildrenMultiple() {
let one = mockService
.uponReceiving("a request for animals with children")
.given("animals have children")
.withRequest(method: .GET, path: "/animals1")
.willRespondWith(
status: 200,
body: [
"animals": Matcher.EachLike(
[
"children": Matcher.EachLike(
Matcher.SomethingLike("Mary"),
min: 0
),
]
)
]
)
let two = mockService
.uponReceiving("a request for animals with Children")
.given("animals have children")
.withRequest(method: .GET, path: "/animals2")
.willRespondWith(
status: 200,
body: [
"animals": Matcher.EachLike(
[
"children": Matcher.EachLike(
Matcher.SomethingLike("Mary"),
min: 0
),
]
)
]
)
mockService.run(verify: [one, two]) { [unowned self] url, done in
let urlOne = URL(string: "\(url)/animals1")!
let urlTwo = URL(string: "\(url)/animals2")!
let expOne = expectation(description: "one")
let expTwo = expectation(description: "two")
session
.dataTask(with: urlOne) { data, response, error in
guard
error == nil,
(response as? HTTPURLResponse)?.statusCode == 200
else {
fail(function: #function, request: urlOne.absoluteString, response: response.debugDescription, error: error)
return
}
// asserts
expOne.fulfill()
}
.resume()
session
.dataTask(with: urlTwo) { data, response, error in
guard
error == nil,
(response as? HTTPURLResponse)?.statusCode == 200
else {
fail(function: #function, request: urlOne.absoluteString, response: response.debugDescription, error: error)
return
}
// asserts
expTwo.fulfill()
}
.resume()
waitForExpectations(timeout: 5) { _ in
done()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment