Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eq matcher produces confusing Diff message for a trailing new line #1408

Closed
aeroastro opened this issue Mar 3, 2023 · 1 comment
Closed

Comments

@aeroastro
Copy link

RSpec's built-in eq matcher is very useful for comparing two strings.
The Diff message for failing test allows developers to quickly find bugs especially for large size text.

However, I have found a confusing message when the diff is a trailing new line.

It says:

<The diff is empty, are your objects producing identical `#inspect` output?>

Here are the related issues.

Your environment

  • Ruby version: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin19]
  • rspec-expectations version: rspec-expectations (3.12.2)

Steps to reproduce

Following is the test code to reproduce this.

require 'rspec'

def lines_to_text(*args)
  args.join("\n")
end

RSpec.describe '#lines_to_text' do
  subject { lines_to_text('foo', 'bar', 'baz') }

  context 'when there is diff inside string' do
    it 'shows diff elegantly' do
      expect(subject).to eq "foo\n\nbar\nbaz"
    end
  end


  context 'when there is a diff after the string' do
    it 'shows somewhat confusing message' do
      expect(subject).to eq <<~EXPECTED
        foo
        bar
        baz
      EXPECTED
    end
  end
end

Expected behavior

For the second test:

  2) #lines_to_text when there is a diff after the string shows somewhat confusing message
     Failure/Error:
       expect(subject).to eq <<~EXPECTED
         foo
         bar
         baz
       EXPECTED

       expected: "foo\nbar\nbaz\n"
            got: "foo\nbar\nbaz"

       (compared using ==)

       Diff:
       @@ -1,3 +1,4 @@
        foo
        bar
        baz
       +

     # ./spec/sample_spec.rb:19:in `block (3 levels) in <top (required)>'

Actual behavior

For the second test:

  2) #lines_to_text when there is a diff after the string shows somewhat confusing message
     Failure/Error:
       expect(subject).to eq <<~EXPECTED
         foo
         bar
         baz
       EXPECTED

       expected: "foo\nbar\nbaz\n"
            got: "foo\nbar\nbaz"

       (compared using ==)

       Diff:
         <The diff is empty, are your objects producing identical `#inspect` output?>
     # ./spec/sample_spec.rb:19:in `block (3 levels) in <top (required)>'
@JonRowe JonRowe closed this as completed Mar 3, 2023
@JonRowe
Copy link
Member

JonRowe commented Mar 3, 2023

Thanks, this is a duplicate of #570 as you linked to, as the trailing new line produces no diff you get this output. The underlying cause is, (again as you linked to) rspec/rspec-support#70, free free to attempt a PR to fix that.

@JonRowe JonRowe closed this as not planned Won't fix, can't repro, duplicate, stale Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants