To use it, add it to your dev-dependencies in your cargo manifest:
You can now write a pact test using the consumer DSL.
By default, the pact files will be written to
target/pacts. To change this, set the environment variable
Pacts are merged with existing pact files when written. To change this behaviour so that the files
are always overwritten, set the environment variable
Testing message consumers is supported. There are two types: asynchronous messages and synchronous request/response.
Asynchronous messages are you normal type of single shot or fire and forget type messages. They are typically sent to a message queue or topic as a notification or event. With Pact tests, we will be testing that our consumer of the messages works with the messages setup as the expectations in test. This should be the message handler code that processes the actual messages that come off the message queue in production.
The generated Pact file from the test run can then be used to verify whatever created the messages adheres to the Pact file.
Synchronous request/response messages are a form of message interchange were a request message is sent to another service and one or more response messages are returned. Examples of this would be things like Websockets and gRPC.
The consumer test builders support using Pact plugins. Plugins are defined in the Pact plugins project. To use plugins requires the use of Pact specification V4 Pacts.
To use a plugin, first you need to let the builder know to load the plugin and then configure the interaction based on
the requirements for the plugin. Each plugin may have different requirements, so you will have to consult the plugin
docs on what is required. The plugins will be loaded from the plugin directory. By default, this is
the value of the
PACT_PLUGIN_DIR environment variable.
There are generic functions that take JSON data structures and pass these on to the plugin to
setup the interaction. For request/response HTTP interactions, there is the
contents function on the request and
response builders. For message interactions, the function is called
For example, if we use the CSV plugin from the plugins project, our test would look like: