This is an example of using the Pact gem for consumer-driven contract testing.
If you're new to consumer-driven contract (CDC) testing, then I suggest the following resources:
Note this repo contains two separate projects with their own Gemfiles. In the real world, of course, the API service and any clients would normally reside in their own repos.
Quiz API
is our service provider for this example. It's a simple Sinatra app.
I chose not to add a database service, though the Quiz API app would certainly need one in order to function. I feel the existing code is sufficient for an example of Pact. If you can't stand failing specs, then feel free to add a database. :)
Quiz Web
consumes the Quiz API
service. You can observe its functionality
in the files consumer/quiz.rb
and consumer/quiz_service_client.rb
.
git clone
this repo to your machine.cd pact-poc
and familiarize yourself with both the consumer and producercd consumer/ && bundle
cd ../provider/ && bundle
After having completed the Setup instructions above, you're ready to generate and verify pacts.
- Generate your consumer's pact file:
cd consumer/ && bundle exec rspec
- Observe the shiny new pact located inside
consumer/spec/pacts/
- Also observe the useful logs located inside
consumer/log/
- Verify the pact against the provider's API:
cd ../provider/ && bundle exec rake pact:verify
- Now observe Pact's auto-generated logs for the provider:
provider/log/
Because we don't have a running database, the pact verification will fail. That's OK. Check out the helpful error output in the console! Pact is pretty awesome.
This is useful too: provider/reports/pacts/help.md
.
Open provider/quiz_api.rb
. See the two defined routes? Try adding an RSpec
specification to the consumer that will generate a pact against the /
route.
You can get this one to pass without a running database.
Pact has Javascript, Go, Swift, and .NET libraries too, among others. See https://docs.pact.io/ for details.