This project is an example of how to use pact-consumer-swift framework.
Install the pact-mock_service
In your terminal run:
brew tap pact-foundation/pact-ruby-standalone
brew install pact-ruby-standalone
This pact mock service will stand in for your API provider and run on localhost.
- Clone the repo
- Sort out the dependencies
- Navigate to the root folder
- run
./scripts/build_test.sh
to run Pact tests - run the app
.build/debug/PactConsumer
(the executable will callhttps://swapi.co/api/people/1/
print the progress and eventually the result returned from the API)
swift package init --type executable
This is an example from this project where Alamofire is used to make the network call:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "PactSwiftPMExample",
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.8.2"),
.package(url: "https://github.com/DiUS/pact-consumer-swift", from: "0.6.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "PactConsumer",
dependencies: ["PactSwiftPMExample"],
path: "Sources/PactConsumer"),
.target(
name: "PactSwiftPMExample",
dependencies: ["Alamofire"]),
.testTarget(
name: "PactSwiftPMExampleTests",
dependencies: ["PactSwiftPMExample", "PactConsumerSwift"]),
]
)
Write your application...
mkdir myProject && cd myProject
,swift package init --type executable
- Resolve dependencies by running
swift package resolve
in your project's root, - Add your
Tests
and edit yourSources
, - Build the project by running
swift build
, - Check if the app works by running
.build/debug/PactConsumer
(that's the target holdingmain.swift
file) - by default it should print a response from https://swapi.co/api/people/1/, - Start up the pact mock service by running
pact-mock-service start --pact-specification-version 2.0.0 --log "./tmp/pact.log" --pact-dir "./tmp/pacts" -p 1234
(or if you're lazy justpact-mock-service start
), - Run your tests by running
swift test
, - Stop the mock service by running
pact-mock_service stop
- When your tests pass, you can find your pact contract file in
./tmp/pacts/_my-provider_-_my-consumer_.json
file
(where provider and consumer is what we set it up in the PactTestsstarWarsProvider = PactConsumerSwift.MockService(provider: "_my-provider_", consumer: "_my-consumer_")
- if you were wondering...)
Following best practices, you should write and run your unit tests before checking if the app works, do you agree?
To avoid running 4 commands to start the mock service, build, test and shut down the mock service, there's a scripts/build_test.sh
script in this repo that takes care of creating the ./tmp
folder, starting up the pact-mock-service, building the project, running the test, and then shutting down the service.
You should probably set up your own test script/s based on your own requirements.