From 1da9a5c9f69a1d04c1eef555b971b687918a716f Mon Sep 17 00:00:00 2001 From: Simon Nizov Date: Sun, 1 Mar 2020 11:47:32 +0200 Subject: [PATCH] feat: Set expected interactions on mock service but without writing them to pact file --- lib/pact/consumer/consumer_contract_builder.rb | 4 ++++ lib/pact/consumer/interaction_builder.rb | 6 ++++++ .../lib/pact/consumer/interaction_builder_spec.rb | 15 +++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/lib/pact/consumer/consumer_contract_builder.rb b/lib/pact/consumer/consumer_contract_builder.rb index 45faa9bc..be556d6b 100644 --- a/lib/pact/consumer/consumer_contract_builder.rb +++ b/lib/pact/consumer/consumer_contract_builder.rb @@ -25,6 +25,10 @@ def initialize(attributes) @mock_service_base_url = "http://#{attributes[:host]}:#{attributes[:port]}" end + def without_writing_to_pact + interaction_builder.without_writing_to_pact + end + def given(provider_state) interaction_builder.given(provider_state) end diff --git a/lib/pact/consumer/interaction_builder.rb b/lib/pact/consumer/interaction_builder.rb index 7916b712..78398ea7 100644 --- a/lib/pact/consumer/interaction_builder.rb +++ b/lib/pact/consumer/interaction_builder.rb @@ -13,6 +13,12 @@ def initialize &block @callback = block end + def without_writing_to_pact + interaction.metadata ||= {} + interaction.metadata[:write_to_pact] = false + self + end + def upon_receiving description @interaction.description = description self diff --git a/spec/lib/pact/consumer/interaction_builder_spec.rb b/spec/lib/pact/consumer/interaction_builder_spec.rb index a16ab53c..0927da9e 100644 --- a/spec/lib/pact/consumer/interaction_builder_spec.rb +++ b/spec/lib/pact/consumer/interaction_builder_spec.rb @@ -88,6 +88,21 @@ module Consumer subject.will_respond_with response end end + + describe "without_writing_to_pact" do + it "sets the write_to_pact key to false on metadata" do + mock_metadata = {} + expect(interaction).to receive(:metadata).and_return(nil, mock_metadata) + + subject.without_writing_to_pact + + expect(mock_metadata).to eq({ write_to_pact: false }) + end + + it "returns itself" do + expect(subject.without_writing_to_pact).to be(subject) + end + end end end end