From 9fdcc579583570b14e8477bffd07c34afc8009a5 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 15 Nov 2018 09:25:48 +1100 Subject: [PATCH] tests: rubyize specs --- spec/lib/pact_broker/pacts/merger_spec.rb | 69 +++++++++++------------ 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/spec/lib/pact_broker/pacts/merger_spec.rb b/spec/lib/pact_broker/pacts/merger_spec.rb index c943308bf..9814054f6 100644 --- a/spec/lib/pact_broker/pacts/merger_spec.rb +++ b/spec/lib/pact_broker/pacts/merger_spec.rb @@ -29,43 +29,40 @@ module Pacts end describe "#merge" do - - before :each do - @pact_to_merge = load_json_fixture('consumer-provider.json') - end + let(:pact_to_merge) { load_json_fixture('consumer-provider.json') } it "merges two pacts" do - @pact_to_merge["interactions"] << example_interaction - result = merge_pacts(example_pact, @pact_to_merge) + pact_to_merge["interactions"] << example_interaction + result = merge_pacts(example_pact, pact_to_merge) expect(result["interactions"]).to match_array(example_pact["interactions"].push(example_interaction)) end it "is idempotent" do - @pact_to_merge["interactions"] << example_interaction - first_result = merge_pacts(example_pact, @pact_to_merge) - second_result = merge_pacts(first_result, @pact_to_merge) + pact_to_merge["interactions"] << example_interaction + first_result = merge_pacts(example_pact, pact_to_merge) + second_result = merge_pacts(first_result, pact_to_merge) expect(first_result).to contain_hash second_result end it "overwrites identical interactions" do - @pact_to_merge["interactions"][0]["response"]["body"] = "changed!" - result = merge_pacts(example_pact, @pact_to_merge) + pact_to_merge["interactions"][0]["response"]["body"] = "changed!" + result = merge_pacts(example_pact, pact_to_merge) expect(result["interactions"].length).to eq example_pact["interactions"].length expect(result["interactions"].first["response"]["body"]).to eq "changed!" end it "appends interactions with a different provider state" do - @pact_to_merge["interactions"][0]["provider_state"] = "upside down" + pact_to_merge["interactions"][0]["provider_state"] = "upside down" - result = merge_pacts(example_pact, @pact_to_merge) + result = merge_pacts(example_pact, pact_to_merge) expect(result["interactions"].length).to eq example_pact["interactions"].length + 1 end it "appends interactions with a different description" do - @pact_to_merge["interactions"][0]["description"] = "getting $$$" + pact_to_merge["interactions"][0]["description"] = "getting $$$" - result = merge_pacts(example_pact, @pact_to_merge) + result = merge_pacts(example_pact, pact_to_merge) expect(result["interactions"].length).to eq example_pact["interactions"].length + 1 end @@ -78,59 +75,57 @@ def merge_pacts(a, b, return_hash = true) end describe "#conflict?" do - before :each do - @pact_to_compare = load_json_fixture('consumer-provider.json') - end + let(:pact_to_compare) { load_json_fixture('consumer-provider.json') } it "returns false if interactions have different descriptions" do - @pact_to_compare["interactions"][0]["description"] = "something else" + pact_to_compare["interactions"][0]["description"] = "something else" - expect(compare_pacts(example_pact, @pact_to_compare)).to eq false + expect(compare_pacts(example_pact, pact_to_compare)).to eq false end it "returns false if interactions have different provider states" do - @pact_to_compare["interactions"][0]["provider_state"] = "some other thing" - expect(compare_pacts(example_pact, @pact_to_compare)).to eq false + pact_to_compare["interactions"][0]["provider_state"] = "some other thing" + expect(compare_pacts(example_pact, pact_to_compare)).to eq false end context "when interactions have the same desc/state" do it "returns false if request parameters are the same" do - expect(compare_pacts(example_pact, @pact_to_compare)).to eq false + expect(compare_pacts(example_pact, pact_to_compare)).to eq false end it "returns true if requests have a different query" do - @pact_to_compare["interactions"][0]["request"]["query"] = "foo=bar&baz=qux" - expect(compare_pacts(example_pact, @pact_to_compare)).to eq true + pact_to_compare["interactions"][0]["request"]["query"] = "foo=bar&baz=qux" + expect(compare_pacts(example_pact, pact_to_compare)).to eq true end it "returns true if requests have a different body" do - @pact_to_compare["interactions"][0]["request"]["body"] = { "something" => { "nested" => "deeply" } } - expect(compare_pacts(example_pact, @pact_to_compare)).to eq true + pact_to_compare["interactions"][0]["request"]["body"] = { "something" => { "nested" => "deeply" } } + expect(compare_pacts(example_pact, pact_to_compare)).to eq true end it "returns true if request method is different" do - @pact_to_compare["interactions"][0]["request"]["method"] = "post" - expect(compare_pacts(example_pact, @pact_to_compare)).to eq true + pact_to_compare["interactions"][0]["request"]["method"] = "post" + expect(compare_pacts(example_pact, pact_to_compare)).to eq true end it "returns true if request path is different" do - @pact_to_compare["interactions"][0]["request"]["path"] = "/new_path" - expect(compare_pacts(example_pact, @pact_to_compare)).to eq true + pact_to_compare["interactions"][0]["request"]["path"] = "/new_path" + expect(compare_pacts(example_pact, pact_to_compare)).to eq true end it "returns true if request headers are different" do - @pact_to_compare["interactions"][0]["request"]["headers"]["Content-Type"] = "text/html" - expect(compare_pacts(example_pact, @pact_to_compare)).to eq true + pact_to_compare["interactions"][0]["request"]["headers"]["Content-Type"] = "text/html" + expect(compare_pacts(example_pact, pact_to_compare)).to eq true end it "returns true if request has additional headers" do - @pact_to_compare["interactions"][0]["request"]["headers"]["Accept"] = "text/html" - expect(compare_pacts(example_pact, @pact_to_compare)).to eq true + pact_to_compare["interactions"][0]["request"]["headers"]["Accept"] = "text/html" + expect(compare_pacts(example_pact, pact_to_compare)).to eq true end it "returns true if request has missing headers" do - @pact_to_compare["interactions"][0]["request"]["headers"].delete("Content-Type") - expect(compare_pacts(example_pact, @pact_to_compare)).to eq true + pact_to_compare["interactions"][0]["request"]["headers"].delete("Content-Type") + expect(compare_pacts(example_pact, pact_to_compare)).to eq true end end