Rspec: nil.should_not be_nil 在我预期失败时通过。为什么?

zor*_*orn 4 unit-testing rspec ruby-on-rails

在 Rails 项目中做一些 rspec 工作。有点好奇为什么这些 should_not be_nil 期望会通过。有什么想法吗?

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
Run Code Online (Sandbox Code Playgroud)

更新:根据这里的反馈,我已经能够找出造成这种情况的原因(来自spec_helper. 我相信这是由于 swizzling.nil?.blank?我将与开发人员交谈以查看这些覆盖是否真的有必要。

感谢那些帮助验证我的东西的人。

ern*_*son 5

我无法重现您的陈述。我在像这样的现有项目中用伪造的规范编写了它们。

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
Run Code Online (Sandbox Code Playgroud)

它返回 - 正如预期的 - 以下输出:

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)
Run Code Online (Sandbox Code Playgroud)

所以也许你的 spec_helper 暗示了什么,这会刺激你的规范。