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

标签 ruby-on-rails unit-testing rspec

在 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/

相关文章:

ruby-on-rails - Ruby 中的绝对符号是什么意思?

ruby-on-rails - 在 SCSS 中访问 Rails 模型或助手

ruby-on-rails - 测试 Stripe 适用于 js : true,,但它会使其他组件无法正常工作

ruby-on-rails - 无法理解 Link_to

python - 我如何指定一个数据库供 Django 测试使用,而不是每次都构建它?

c# - Observable.FromAsync 和 Observable.Switch 的单元测试失败

android - 机器人电动 1.2 : “WARNING: Unable to find path to Android SDK”

ruby-on-rails - PG::错误:错误:关系 "users"不存在

ruby-on-rails - 来自真实世界应用程序的 rails rspec 代码 (/spec)

ruby-on-rails - has_one嵌套路由和路由报错信息: "No route matches [POST] "/users/3/profiles""