Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service p
Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service p
pact-ruby v2+ implements support for the latest versions of Pact specifications:
The pact/v2 namespace was introduced in pact-ruby v1.67.0 and moved to pact in v2
It introduces a suite of new depedencies, including a reliance on the pact-ffi and ffi gems.
gem "pact"pact-ffi ships prebuilt binary gems, and does not support platforms outside of the released pact_ffi libraries
| Version | Platform |
|---|---|
| 0.5.8.1 | x86_64-darwin |
| 0.5.8.1 | arm64-darwin |
| 0.5.8.1 | x86_64-linux |
| 0.5.8.1 | aarch64-linux |
| 0.5.8.1 | x86_64-linux-musl |
| 0.5.8.1 | aarch64-linux-musl |
| 0.5.8.1 | x64-mingw32 |
| 0.5.8.1 | x64-mingw-ucrt |
If you require a pure ruby gem, you are advised to pin to v1.
For each type of interaction (due to their specific features), a separate version of DSL has been implemented. However, the general principles remain the same for each type of interaction.
Place your consumer tests under
spec/pact/providers/**
it's not an error: consumer tests contain providers subdirectory (because we're testing against different providers)
…Common DSL Methods:
new_interaction - initializes a new interactiongiven - allows specifying a provider state with or without parameters, for more details see provider_statesupon_receiving - allows specifying the name of the interactionreference - Allows recording of an interaction reference to support additional features such as Bi-Directional contract testing with asyncapiMultiple interactions can be declared within a single rspec example, in order to call the mock server
execute_http_pact: Use this instead of interaction.executeSpecific DSL methods:
with_request({method: string, path: string, headers: kv_hash, body: kv_hash}) - request definitionwill_respond_with({status: int, headers: kv_hash, body: kv_hash}) - response definitionMore at http_client_spec.rb
Specific DSL methods:
with_service(PROTO_PATH, RPC_SERVICE_AND_ACTION) - specifies the contract used, PROTO_PATH is relative from the app rootwith_request(request_kv_hash) - request definitionwill_respond_with(response_kv_hash) - response definitionMore at grpc_client_spec.rb
Specific DSL methods:
with_headers(kv_hash) - message-headers definition; you can use matcherswith_metadata(kv_hash) - message-metadata definition (special keys are key and topic, where, respectively, you can specify the matchers for the partitioning key and the topicNext, the specifics are one of two options for describing the format:
JSON (to describe a message in a JSON representation):
with_json_contents(kv_hash) - message format definitionPROTO (to describe the message in the protobuf view):
with_proto_class(PROTO_PATH, PROTO_MESSAGE_NAME) - specifies the contract used, PROTO_PATH is relative to the root, PROTO_MESSAGE_NAME is the name of the message used from the proto filewith_proto_contents(kv_hash) - message format definitionMore at message_spec.rb
Specific DSL methods:
with_headers(kv_hash) - message-headers definition; you can use matcherswith_metadata(kv_hash) - message-metadata definition (special keys are key and topic, where, respectively, you can specify the matchers for the partitioning key and the topicNext, the specifics are one of two options for describing the format:
JSON (to describe a message in a JSON representation):
with_json_contents(kv_hash) - message format definitionPROTO (to describe the message in the protobuf view):
with_proto_class(PROTO_PATH, PROTO_MESSAGE_NAME) - specifies the contract used, PROTO_PATH is relative to the root, PROTO_MESSAGE_NAME is the name of the message used from the proto filewith_proto_contents(kv_hash) - message format definitionMore at kafka_spec.rb
Requires the following gems, to use this wrapper
Matchers are special helper methods that allow you to define rules for matching request/response parameters at the level of the pact manifest. The matchers are described in the Pact specifications. In this gem, the matchers are implemented as RSpec helpers.
For details of the implementation, see matchers.rb
match_exactly(sample) - match the exact value specified in the samplematch_type_of(sample) - match the data type (integer, string, boolean) specified in the samplematch_include(sample) - match a substringmatch_any_string(sample) - match any string, because of the peculiarities, null and empty strings will also be matched herematch_any_integer(sample) - match any integermatch_any_decimal(sample) - match any float/doublematch_any_number(sample) - match any integer/float/doublematch_any_boolean(sample) - match any true/falsematch_uuid(sample) - match any UUID (match_regex is used under the hood)match_regex(regex, sample) - match by regexpmatch_datetime(format, sample) - match any datetimematch_iso8601(sample) - match datetime in ISO8601 (the matcher does not fully comply with ISO8601, matches only the most common variants, match_regex is used under the hood)match_date(format, sample) - match any date (rust datetime)match_time(format, sample) - match any time (rust datetime)match_each(template) - match all the elements of the array according to the specified template, you can use it for nested elementsmatch_each_regex(regex, sample) - match all array elements by regex, used for arrays with string elementsmatch_each_key(template, key_matchers) - match each hash key according to the specified templatematch_each_value(template) - match each hash value according to the specified template, can be used for nested elementsmatch_each_kv(template, key_matchers) - match all the keys/values of Hash according to the specified template and key_matchers, can be used for nested elementsSee the different uses of the matchers in matchers_spec.rb
Generators are helper methods that allow you to specify dynamic values in your contract tests. These values are generated at runtime, making your contracts more flexible and robust. Below are the available generator methods:
For details of the implementation, see matchers.rb
generate_random_int(min:, max:) - Generates a random integer between the specified min and max.generate_random_decimal(digits:) - Generates a random decimal number with the specified number of digits.generate_random_hexadecimal(digits:) - Generates a random hexadecimal string with the specified number of digits.generate_random_string(size:) - Generates a random string of the specified size.generate_uuid(example: nil) - Generates a random UUID. Optionally, provide an example value.generate_date(format: nil, example: nil) - Generates a date string in the specified format. Optionally, provide an example.generate_time(format: nil) - Generates a time string in the specified format.generate_datetime(format: nil) - Generates a datetime string in the specified format.generate_random_boolean - Generates a random boolean value (true or false).generate_from_provider_state(expression:, example:) - Generates a value from the provider state using the given expression and example value. Allows templating of url and query paths with values only know at provider verification time.generate_mock_server_url(regex: nil, example: nil) - Generates a mock server URL. Optionally, specify a regex matches and/or an example value.These generators can be used in your DSL definitions to provide dynamic values for requests, responses, or messages in your contract tests.
…Place your provider verification file under
spec/pact/consumers/**
it's not an error: provider tests contain consumers subdirectory (because we're verifying against different consumer)
……You may have a consumer pact which consumes multiple transport protocols, if they are using pact specification v4.
In order to validate an entire pact in a single test run, you will need to configure each transport as appropriate.
…The following issues were present in pact 2.0.0 and can be worked around in a pact_helper.rb loaded before pact/rspec.
activesupport dependencypact 2.0.0 uses present? and blank? from ActiveSupport internally without declaring it as a gem dependency. This raises a NoMethodError when ActiveSupport is not loaded by the host application.
Workaround: Require the ActiveSupport extension before pact:
require 'active_support/core_ext/object/blank'If your application does not already pull in activesupport, add it to your Gemfile:
gem 'activesupport', '>= 6.0', require: falserack-proxy calls body_stream.rewind on Rackup::Handler::WEBrick::Inputrack-proxy 0.7.7 (a transitive dependency of pact) calls body_stream.rewind after assigning a Rackup::Handler::WEBrick::Input. Under rackup 2 / rack 3, that class does not implement rewind, raising a NoMethodError during provider verification setup.
Workaround: Patch the class with a no-op rewind in your pact_helper.rb. The guard ensures it only applies when the method is absent:
require 'rackup/handler/webrick'
Rackup::Handler::WEBrick::Input.class_eval { def rewind; end unless method_defined?(:rewind) }WebmockHelpers.turned_off missing return before early yieldWebmockHelpers.turned_off in pact 2.0.0 is missing a return before the early yield that guards against WebMock not being defined. Without it the method falls through and calls WebMock::Config, raising a NameError when WebMock is absent.
Workaround: Require webmock before pact so that defined?(::WebMock) is truthy and the correct code path runs:
require 'webmock'Add webmock to your Gemfile if not already present:
gem 'webmock', require: falsepact_helper.rbIf you encounter all three issues, here is a single pact_helper.rb that applies every workaround. Require this file before pact/rspec in your provider verifica
No open issues yet, or sync has not completed.