arealhumanbean
arealhumanbean

Reputation: 82

Ruby/Rspec: should be_false vs should == false

Here's my code:

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

And here's the test I'm running it against:

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

Now, that test will fail. However, if I change it to

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

then it passes.

How can I change my code so that should be_false passes instead of should == false? Thanks.

Upvotes: 1

Views: 717

Answers (1)

Santhosh
Santhosh

Reputation: 29144

be_false matches falsey values (nil and false) and be_true matches truthy values (other than nil or false)

From Rspec > 3.0,

be_false is renamed to be_falsey and

be_true is renamed to be_truthy

If you want to exactly match false, you should use

obj.should eq false

See the Documentation for more info about 'be' matchers

Upvotes: 7

Related Questions