Reputation: 438
Doing some rspec work in a Rails project. Kind curious why these should_not be_nil expectations pass. Any thoughts?
it "nils should be nil" do
@io = nil
@io.should_not be_nil #passes (shouldn't!!)
nil.should_not be_nil #passes (shouldn't!!)
@io.should == nil # passes
@io.should be_nil # passes
@io.nil?.should be_true # passes
#@io.nil?.should be_false # fails as expected
end
UPDATE: Based on feedback here I've been able isolate the cause of this (which is from things loaded in the spec_helper
). I believe it's due to a poor swizzling of .nil?
or .blank?
which I'm going to have a talk to the dev to see if these overrides are really necessary at all.
Thanks to those who helped verify my stuff.
Upvotes: 4
Views: 21682
Reputation: 1764
I can not reproduce your statements. I wrote them in a fake spec in an existing project like this.
require 'spec_helper'
describe "Pepper" do
describe "simplicity" do
before(:each) { @io = nil }
# should fail
it 'should be nil 1' do
@io.should_not be_nil
end
# should fail
it 'should be nil 2' do
nil.should_not be_nil
end
# should NOT fail
it 'should be nil 3' do
@io.should == nil
end
# should NOT fail
it 'should be nil 4' do
@io.should be_nil
end
# should NOT fail
it 'should be nil 5' do
@io.nil?.should be_true # passes
end
# should fail
it 'should be nil 6' do
@io.nil?.should be_false
end
end
end
which returns - as expected - the following output:
simplicity
should be nil 1 (FAILED - 1)
should be nil 2 (FAILED - 2)
should be nil 3
should be nil 4
should be nil 5
should be nil 6 (FAILED - 3)
So maybe your spec_helper is implying something, that is irritating your specs.
Upvotes: 7