You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Running this demo project
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 call https://swapi.co/api/people/1/ print the progress and eventually the result returned from the API)
Your Project
Init your brand new executable Swift project
swift package init --type executable
Prepare your dependencies in Package.swift manifest file
This is an example from this project where Alamofire is used to make the network call:
// swift-tools-version:4.0
import PackageDescription
letpackage=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...
Overall Workflow Example
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 your Sources,
Build the project by running swift build,
Check if the app works by running .build/debug/PactConsumer (that's the target holding main.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 just pact-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...)
Notes
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.
About
An example project using pact-consumer-swift library using Swift Package Manager