Pact test DSL for writing consumer pact tests in Rust
This library provides a test DSL for writing consumer pact tests in Rust. It is based on the
V3 pact specification.
Online rust docs
To use it
To use it, add it to your dev-dependencies in your cargo manifest:
[dev-dependencies]
pact_consumer = "0.6.3"
You can now write a pact test using the consumer DSL.
use pact_consumer::prelude::*;
use pact_consumer::*;
#[test]
fn a_service_consumer_side_of_a_pact_goes_a_little_something_like_this() {
let pact_runner = ConsumerPactBuilder::consumer(s!("Consumer")) .has_pact_with(s!("Alice Service")) .given("there is some good mallory".to_string()) .upon_receiving("a retrieve Mallory request".to_string()) .path(s!("/mallory")) .will_respond_with() .status(200)
.headers(hashmap!{ s!("Content-Type") => s!("text/html") })
.body(OptionalBody::Present(s!("That is some good Mallory.")))
.build();
let result = pact_runner.run(&|url| {
let client = Client { url: url.clone(), .. Client::default() }; let result = client.fetch("/mallory"); expect!(result).to(be_ok().value("That is some good Mallory."));
Ok(())
});
expect!(result).to(be_equal_to(VerificationResult::PactVerified)); }
Changing the output directory
By default, the pact files will be written to target/pacts
. To change this, set the environment variable PACT_OUTPUT_DIR
.
Forcing pact files to be overwritten
Pacts are merged with existing pact files when written. To change this behaviour so that the files
are always overwritten, set the environment variable PACT_OVERWRITE
to true
.