Testing 'nil.should == something_not_nil' fails

Hello,

After upgrading to rspec 2.0.0.rc, I ran into an issue that I reported
at http://github.com/rspec/rspec-core/issues/issue/183/#issue/183. When
testing something that returned nil, effectively executing ‘nil.should
== something_not_nil’, rspec would come crashing down. It turned out not
to be a bug in rspec (or, at least, David can’t reproduce it).

I initially wrote it off as something strange with my setup and worked
around it for now (specs usually succeed, so nil == nil and everything
works fine), but as ‘scrymmyin’ reported seeing the same behavior in
issue 183, and as I also see it with rspec 2.0.0, I thought it might be
nice to document it here.

I can reproduce the behavior as follows:

  • gem install rspec --version=2.0.0
  • mkdir sandbox; cd sandbox
  • create Gemfile with contents
    gem ‘rspec’, ‘2.0.0’
  • run bundle install
  • mkdir spec
  • create spec/nil_fail.rb with contents
    describe ‘Sandbox’ do
    it ‘should show the difference’ do
    nil.should == ‘foo’
    end
    end
  • run rspec spec/nil_fail.rb

The result is:

[email protected]:~/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 $REE_path/bin/rspec:19

best regards,

On Oct 13, 2010, at 10:02 AM, Ivo W. wrote:

works fine), but as ‘scrymmyin’ reported seeing the same behavior in

$REE_gems_path/rspec-core-2.0.0.rc/lib/rspec/core/formatters/base_formatter.rb:122:in

from
each'run_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 $REE_path/bin/rspec:19

FYI - setting an expectation on nil was a red herring. The example could
have failed for any other reason (“foo”.should eq(“bar”)) and the same
error would have occurred.

The problem was actually related to the filename. While rspec was able
to load up the file named outside the _spec.rb convention (i.e.
nil_fail.rb), it was not able to correctly report a failure in that
file.

Fixed:
http://github.com/rspec/rspec-core/commit/9c6b6f74da382d0810ab337730828c5fd94e309b

To be released in 2.0.1.

Cheers,
David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs