Skip to content

Instantly share code, notes, and snippets.

@bethesque
Last active August 29, 2015 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bethesque/e7a1de2f38eeb40d4b3e to your computer and use it in GitHub Desktop.
Save bethesque/e7a1de2f38eeb40d4b3e to your computer and use it in GitHub Desktop.
Write a pact without a consumer

This is an example to show how you might create a pact to test a provider without actually writing a consumer.

To execute: bundle exec ruby run.rb

This uses pact private APIs, and so can't be guaranteed to work with anything other than versions ~> 1.7.0

source 'https://rubygems.org'
gem 'pact', '~>1.7.0'
require 'pact'
module Pact
class DummyConsumerContractBuilder
def initialize args
@interactions = []
@consumer_name = args.fetch(:consumer)
@provider_name = args.fetch(:provider)
@pact_dir = args.fetch(:pact_dir)
end
def given(provider_state)
interaction_builder.given(provider_state)
end
def upon_receiving(description)
interaction_builder.upon_receiving(description)
end
def interaction_builder
@interaction_builder ||=
begin
interaction_builder = Pact::Consumer::InteractionBuilder.new do | interaction |
self.handle_interaction_fully_defined(interaction)
end
interaction_builder
end
end
def write_pact
consumer_contract_writer = Pact::ConsumerContractWriter.new(
{
interactions: @interactions,
consumer: {name: @consumer_name},
provider: {name: @provider_name},
pact_dir: @pact_dir
},
Logger.new($stdout)
)
consumer_contract_writer.write
end
def handle_interaction_fully_defined interaction
@interactions << interaction
@interaction_builder = nil
end
end
end
some_provider = Pact::DummyConsumerContractBuilder.new(
consumer: 'Consumer',
provider: 'Provider',
pact_dir: './tmp/pacts'
)
some_provider
.upon_receiving('a request for foo')
.with(method: 'POST', path: '/foo')
.will_respond_with(
status: 200,
body: Pact::SomethingLike.new({'name' => 'foo'})
)
puts some_provider.write_pact
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment