Skip to content

Commit

Permalink
fix: diff message when actual 'array like' is too long
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 29, 2017
1 parent ad7c340 commit 68be738
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/pact/matchers/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,19 @@ def value_difference_message expected, actual, options = {}
end

def type_difference_message expected, actual
case actual
when Pact::IndexNotFound
"Actual array is too short and should have contained #{short_description(expected)} at <path>"
case expected
when Pact::UnexpectedIndex
"Actual array is too long and should not contain #{short_description(actual)} at <path>"
else
expected_desc = class_name_with_value_in_brackets(expected)
expected_desc.gsub!("(", "(like ")
actual_desc = class_name_with_value_in_brackets(actual)
message = "Expected #{expected_desc} but got #{actual_desc} at <path>"
case actual
when Pact::IndexNotFound
"Actual array is too short and should have contained #{short_description(expected)} at <path>"
else
expected_desc = class_name_with_value_in_brackets(expected)
expected_desc.gsub!("(", "(like ")
actual_desc = class_name_with_value_in_brackets(actual)
message = "Expected #{expected_desc} but got #{actual_desc} at <path>"
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/lib/pact/matchers/matchers_messages_hash_and_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ module Pact::Matchers
end
end
end

context "with an ArrayLike" do
context "with an extra item that is an Integer" do
let(:expected) { {thing: Pact.like([1])} }
let(:actual) { {thing: [1, 2]} }
let(:difference) { diff(expected, actual) }

it "returns a message" do
expect(difference[:thing][1].message).to eq "Actual array is too long and should not contain 2 at <path>"
end
end
end
end
end
end

0 comments on commit 68be738

Please sign in to comment.