Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Notice when object implement inspect but == is different (#1126)
Browse files Browse the repository at this point in the history
* Notice when object implements identical `#inspect` but == returns false

In a few cases the objects under test implement identical `inspect` output but the `eq` matcher
will see a difference when doing `==`.

This can lead to misleading output like this:

```
Failures:

  1) Foo confuses users with an empty diff
     Failure/Error: Foo.something(Foo.new)

       #<Foo (class)> received :something with unexpected arguments
         expected: ("foobar")
              got: ("foobar")
       Diff:

     # ./spec/foo_spec.rb:13:in `block (2 levels) in <top (required)>'
```

This change adds a notice to help to understand this failed expectation.

Fix: rspec/rspec-support#274
  • Loading branch information
benoittgt authored and JonRowe committed Oct 2, 2019
1 parent 36109e3 commit 81ea0bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/rspec/matchers/expecteds_for_multiple_diffs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def diffs(differ, actual)
@expected_list.map do |(expected, diff_label)|
diff = differ.diff(actual, expected)
next if diff.strip.empty?
"#{diff_label}#{diff}"
if diff == "\e[0m\n\e[0m"
"#{diff_label}\n" \
" <The diff is empty, are your objects producing identical `#inspect` output?>"
else
"#{diff_label}#{diff}"
end
end.compact.join("\n")
end
end
Expand Down
6 changes: 4 additions & 2 deletions spec/rspec/matchers/expecteds_for_multiple_diffs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ def create_matcher(stubs)
end

describe "#message_with_diff" do
it "returns just provided message if diff is empty" do
allow(FakeDiffer).to receive(:diff) { "" }
it "returns a message warning if the diff is empty" do
allow(FakeDiffer).to receive(:diff) { "\e[0m\n\e[0m" }
expect(wrapped_value.message_with_diff(
message, differ, actual
)).to eq(dedent <<-EOS)
|a message
|Diff:
| <The diff is empty, are your objects producing identical `#inspect` output?>
EOS
end

Expand Down

0 comments on commit 81ea0bf

Please sign in to comment.