在 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
更新:根据这里的反馈,我已经能够找出造成这种情况的原因(来自
spec_helper
中加载的内容)。我相信这是由于 .nil?
的错误混合所致或 .blank?
我将与开发人员交谈,看看这些覆盖是否真的有必要。感谢那些帮助验证我的东西的人。
最佳答案
我无法重现您的陈述。我在像这样的现有项目中用伪造的规范编写了它们。
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
它返回 - 正如预期的 - 以下输出:
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)
所以也许你的 spec_helper 暗示了什么,这会刺激你的规范。
关于ruby-on-rails - Rspec: nil.should_not be_nil 在我预期失败时通过。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277376/