Skip to content

Instantly share code, notes, and snippets.

@bethesque
Last active August 29, 2015 14:02
Show Gist options
  • Save bethesque/21f8cf25013ff46b524e to your computer and use it in GitHub Desktop.
Save bethesque/21f8cf25013ff46b524e to your computer and use it in GitHub Desktop.
Using pact for Capybara tests and unit tests
class InternalProviderClient
def thing
#get a thing from the internal provider
end
end
# pact_helper
Pact.configure do | config |
config.pactfile_write_mode = :update
end
Pact.consumer "My Rails App" do
has_pact_with "Internal Provider" do
mock_service :internal_provider do
port 1234
end
end
end
module PactExpectations
#or maybe extend, try and see
#may not need to do this actually, because the builders will be included in the rspec scope where
#these methods will be called.
include Pact::Consumer::ConsumerContractBuilders
def stub_some_request_with_success_response
internal_provider.
given("something exists").
upon_recieving("some request").
with(...).
will_respond_with(...)
end
def stub_some_request_with_error_response
internal_provider.
given("an error occurs retrieving something").
upon_recieving("some request").
with(...).
will_respond_with(...)
end
end
# provider spec
require './spec/service_providers/pact_helper'
describe InternalProviderClient, :pact => true do
include PactExpectations
describe "thing" do
context "when successful" do
before do
stub_some_request_with_some_response
end
it "returns a thing" do
expect(InternalProviderClient.new.thing).to be_a_thing
end
end
context "when an error occurs retrieving the thing" do
before do
stub_some_request_with_error_response
end
it "raises an error" do
expect{ InternalProviderClient.new.thing}.to raise_error /No thing!!/
end
end
end
end
# capybara spec
require './spec/service_providers/pact_helper'
#Are you using Cucumber or RSpec???
describe "using the UI", :pact => true do
include PactExpectations
before do
stub_some_request_with_some_response
end
it "does something when I click" do
#click stuff
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment