【问题标题】:Ruby/Rspec: should be_false vs should == falseRuby/Rspec:应该是_false vs 应该 == false
【发布时间】:2014-08-04 19:13:21
【问题描述】:

这是我的代码:

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?谢谢。

【问题讨论】:

  • 你试过should be_false吗?另外你可能应该使用@d.should include 'fish'
  • @BroiSatse 我很抱歉。 should be_false 是我在原帖中的意思。我已经编辑过了。我想让should be_false 工作。
  • @arealhumanbean - 你使用的是什么 rspec 版本?
  • 如果你使用rspec > 3.0be_false应该改为be_falsey

标签: ruby object rspec


【解决方案1】:

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

从 Rspec > 3.0,

be_false 重命名为be_falsey

be_true 重命名为be_truthy

如果你想完全匹配false,你应该使用

obj.should eq false

有关“be”匹配器的更多信息,请参阅Documentation

【讨论】:

  • 在答案中添加了更多信息。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多