ruby /Rspec : should be_false vs should == false

标签 ruby object rspec

这是我的代码:

class Dictionary
  def entries
    @entries ||= {}
  end

  def add(hash)
    if hash.class == Hash
      hash.each_pair do |k, v|
        entries[k] = v
      end
    else
      makehash = {hash => nil}
      self.add(makehash)
    end
    @entries = entries
  end

  def keywords
    @entries.keys
  end

  def include?(k)
    if @entries == nil
      false
    elsif self.keywords.include?(k)
      true
    else
      false
    end
  end
end

这是我正在运行的测试:

require 'dictionary'

describe Dictionary do
  before do
    @d = Dictionary.new
  end

  it 'can check whether a given keyword exists' do
    @d.include?('fish').should be_false
  end

现在,该测试将失败。但是,如果我将其更改为

  it 'can check whether a given keyword exists' do
    @d.include?('fish').should == false
  end

然后它通过了。

我怎样才能改变我的代码,使 should be_false 而不是 should == false 通过?谢谢。

最佳答案

be_false 匹配虚假值(nilfalse)和 be_true 匹配真值(nilfalse 除外)

来自 Rspec > 3.0,

be_false 重命名为 be_falsey

be_true 重命名为 be_truthy

如果你想精确匹配false,你应该使用

obj.should eq false

参见 Documentation有关“be”匹配器的更多信息

关于 ruby /Rspec : should be_false vs should == false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25116634/

相关文章:

ruby - 从 Ruby 中的国家代码获取表情符号标志

java - 如何创建一个类并添加到 for 循环中的列表中接收来自控制台的输入

r - 在 R 中导入多个文件时自动命名对象

javascript - 如何轻松创建一个 javascript 2D 字典,其中键顺序无关紧要?

ruby-on-rails - 尽管存在链接,RSpec 应该 has_link 失败

ruby-on-rails - 如何在 Rails 3 中创建图表

ruby - EM :Connection (em-synchrony) 内的光纤

ruby-on-rails - 在验收测试中模拟昂贵的资源(rspec, cucumber )

ruby-on-rails - 从 Rails 引擎扩展模型(而不是替换它)

ruby-on-rails - 如何在 Eclipse/Aptana Studio 中调试 ruby​​ 测试?