From 74e956875f157707490701e0938973b26d8146a7 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 24 Jan 2020 09:04:40 +1100 Subject: [PATCH] feat: give each interaction an index when parsing the contract --- .../consumer_contract/http_consumer_contract_parser.rb | 2 +- lib/pact/consumer_contract/interaction.rb | 3 ++- .../http_consumer_contract_parser_spec.rb | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/pact/consumer_contract/http_consumer_contract_parser.rb b/lib/pact/consumer_contract/http_consumer_contract_parser.rb index b555702..e072bf5 100644 --- a/lib/pact/consumer_contract/http_consumer_contract_parser.rb +++ b/lib/pact/consumer_contract/http_consumer_contract_parser.rb @@ -13,7 +13,7 @@ def call(hash) Pact.configuration.error_stream.puts "WARN: This code only knows how to parse v3 pacts, attempting to parse v#{options[:pact_specification_version]} pact using v3 code." end - interactions = hash[:interactions].collect { |hash| Interaction.from_hash(hash, options) } + interactions = hash[:interactions].each_with_index.collect { |hash, index| Interaction.from_hash({ index: index }.merge(hash), options) } ConsumerContract.new( :consumer => ServiceConsumer.from_hash(hash[:consumer]), :provider => ServiceProvider.from_hash(hash[:provider]), diff --git a/lib/pact/consumer_contract/interaction.rb b/lib/pact/consumer_contract/interaction.rb index 40e9cd8..e943f1d 100644 --- a/lib/pact/consumer_contract/interaction.rb +++ b/lib/pact/consumer_contract/interaction.rb @@ -5,7 +5,7 @@ module Pact class Interaction include ActiveSupportSupport - attr_accessor :description, :request, :response, :provider_state, :provider_states, :metadata, :_id + attr_accessor :description, :request, :response, :provider_state, :provider_states, :metadata, :_id, :index def initialize attributes = {} @description = attributes[:description] @@ -15,6 +15,7 @@ def initialize attributes = {} @provider_states = attributes[:provider_states] @metadata = attributes[:metadata] @_id = attributes[:_id] + @index = attributes[:index] end def self.from_hash hash, options = {} diff --git a/spec/lib/pact/consumer_contract/http_consumer_contract_parser_spec.rb b/spec/lib/pact/consumer_contract/http_consumer_contract_parser_spec.rb index b726ff6..b441fc6 100644 --- a/spec/lib/pact/consumer_contract/http_consumer_contract_parser_spec.rb +++ b/spec/lib/pact/consumer_contract/http_consumer_contract_parser_spec.rb @@ -11,6 +11,10 @@ module Pact it "correctly parses the pact" do expect(subject.interactions.first.response.headers['Content-Type']).to be_a(Pact::Term) end + + it "sets the index of each interaction" do + expect(subject.interactions.first.index).to eq 0 + end end context "with a v3 pact" do @@ -19,6 +23,10 @@ module Pact it "correctly parses the pact" do expect(subject.interactions.first.response.body['foo']).to be_a(Pact::SomethingLike) end + + it "sets the index of each interaction" do + expect(subject.interactions.first.index).to eq 0 + end end end end