rspec-core icon indicating copy to clipboard operation
rspec-core copied to clipboard

Failure to render diff for 'nil.should == something_not_nil'

Open Confusion opened this issue 16 years ago • 8 comments

When the thing you are writing a spec for yields nil, and the expected value is different, the error is not what you would hope for:

describe 'Sandbox' do
  it 'should show the difference' do
    nil.should == 'foo'
  end
end

user@machine:~/dev/ruby/sandbox$ rspec spec/nil_fail.rb F

Failures:

  1. Sandbox should show the difference $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_formatter.rb:138:in find_failed_line': undefined methoddowncase' for nil:NilClass (NoMethodError) from $REE_gems_path/rspec-expectations-2.0.0.rc/lib/rspec/expectations/fail_with.rb:29:in detect' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_formatter.rb:136:ineach' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_formatter.rb:136:in detect' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_formatter.rb:136:infind_failed_line' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_formatter.rb:122:in read_failed_line' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_text_formatter.rb:27:indump_failures' from $REE_gems_path/rspec-expectations-2.0.0.rc/lib/rspec/expectations/fail_with.rb:29:in each_with_index' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_text_formatter.rb:17:ineach' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_text_formatter.rb:17:in each_with_index' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_text_formatter.rb:17:indump_failures' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/reporter.rb:74:in send' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/reporter.rb:74:innotify' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/reporter.rb:73:in each' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/reporter.rb:73:innotify' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/reporter.rb:22:in conclude' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/reporter.rb:13:inreport' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/command_line.rb:23:in run' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/runner.rb:55:inrun_in_process' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/runner.rb:46:in run' from $REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/runner.rb:10:inautorun' from rspec:19

Confusion avatar Oct 10 '10 09:10 Confusion

I just pasted your example into a file and ran it against rspec-2.0.0.rc and the current master using ruby 1.8.6, 1.8.7, 1.9.2, ree-1.8.7, and jruby, and got this in every case:

Failures:
  1) Sandbox should show the difference
     Failure/Error: nil.should == 'foo'
     expected: "foo",
          got: nil (using ==)

I'm closing this as I can not reproduce it, but if you are able to provide information I can use to reproduce it I'll be glad to reopen it.

dchelimsky avatar Oct 10 '10 13:10 dchelimsky

Strange, I'm not sure what the difference between your setup and mine might be. This one is Ubuntu Lucid, REE 2010.02

I can reproduce it in a clean environment:

  • gem uninstall rspec rspec-core rspec-expectations rspec-mocks

  • gem install rspec rspec-core rspec-expectations rspec-mocks --pre ('gem install rspec --pre' doesn't pull the other the others along; should that be the case?)

  • Create a new empty directory.

  • Create a Gemfile with one line:

    gem 'rspec', '2.0.0.rc',

  • Run 'bundle install'

  • Create a directory 'spec' with one file, 'nil_fail.rb', with the content from my original post.

  • Run 'rspec spec/nil_fail.rb' or 'bundle exec rspec spec/nil_fail.rb' and it fails.

Do you have any suggestions on what might be wrong over here?

Confusion avatar Oct 10 '10 14:10 Confusion

re: the dependent gems not being installed, I'm guessing you have an older version of rubygems, in which case you should definitely upgrade to 1.3.7.

re: why you're seeing different behaviour from me, I'm stumped. Why don't you post this to the rspec-user's list and we'll see if anyone with the same setup as you can be of any help.

dchelimsky avatar Oct 10 '10 14:10 dchelimsky

Concerning the dependent gems: REE 2010.02 comes with rubygems 1.3.7, so that shouldn't be the problem. I removed all installed gems and after that rspec once again pulls the others along. It seems another gem can get in the way, though I don't know which one it was.

Confusion avatar Oct 10 '10 17:10 Confusion

I also have this issue on Ubuntu maybe it has to do with the gems system on Ubuntu. Confusion please let me know if you find anything out. I will do the same if I find anything.

scrummyin avatar Oct 12 '10 21:10 scrummyin

A little more information is that it is the path argument that is nil.

scrummyin avatar Oct 12 '10 21:10 scrummyin

The fact that the expectation is set on nil is a red herring. The problem is actually the same one that is reported in #190, which is that if the file name doesn't end in _spec.rb, the file location is not stored correctly by rspec, and the error is generated when trying to report the failure.

dchelimsky avatar Oct 13 '10 22:10 dchelimsky

Verified: renaming the file to end in _spec.rb solves the issue and results in the expected diff.

Confusion avatar Oct 14 '10 06:10 Confusion