Skip to content

Instantly share code, notes, and snippets.

@bethesque
Last active August 29, 2015 14:06
Show Gist options
  • Save bethesque/f90993ac35732fdde814 to your computer and use it in GitHub Desktop.
Save bethesque/f90993ac35732fdde814 to your computer and use it in GitHub Desktop.
Idea for FixturePact

Pact is great for HTTP microservices that communicate directly with each other, but many services use an intermediate system that acts as a pipe (eg. a queue, S3 bucket). The agreement about the format of the message is really between the two ends of the chain, not between the links immediatly adjacent.

The pact concept should be able to be applied to non-http services. The code below is an attempt to come up with a DSL for a new pact library that uses the same pact matching concepts, but does not constrain the communication protocol.

Thoughts/improvements appreciated, but remember Gists do not send notifications when you post a comment :|

require 'fixture_pact/consumer'
FixturePact.consumer "Billing Run" do
has_fixture_pact_with "Billable Purchases" do
pact_builder :billable_purchases
end
end
# Repository wrapping S3 bucket
class BillablePurchasesRepository
def self.purchases
JSON.parse(S3.some_object).collect do | purchase_hash |
Purchase.new(purchase_hash)
end
end
end
# Spec for repository
describe BillablePurchasesRepository do
let(:billable_purchases_list) {
billable_purchases
.given("a customer has purchased an online product")
.will_provide("a list of billable purchases")
.with([{amount: 12, date: Pact::Term.new(matcher: /\d\d\/\d\d\/\d\d/, generate: '12/12/12'}])
}
before do
allow(S3).to receive(:some_object).and_return(billable_purchases_list.to_json)
end
it "returns a list of Purchases" do
expect(BillablePurchasesRepository.purchases.size).to eq 1
expect(BillablePurchasesRepository.purchases.first).to be_instance_of(Purchase)
end
end
require 'fixture_pact/provider'
FixturePact.provider "Billable Purchases" do
honours_fixture_pact_with "Billing Run" do
fixture_pact_url "http://blah/blah"
end
end
FixturePact.provider_states_for "Billing Run" do
provider_state "a customer has purchased an online product" do
set_up do
# insert a purchase into something
end
end
end
FixturePact.to_verify "a list of billable purchases" do
BillablePurchases.extract_purchases
end
@bethesque
Copy link
Author

Is there any benefit to using this over JSON schema?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment