Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

ruby - rspec failing error: expected false to respond to `false?`

I am running this portion of a test:

 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

With this code:

class Dictionary
  def initialize
    @hash = {}
  end 

  def add(new_entry)
    new_entry.class == String ? @hash[new_entry] = nil : new_entry.each { |noun, definition| @hash[noun] = definition}    
  end 

  def entries
    @hash 
  end 

  def keywords
    @hash.keys
  end

  def include?(word)
    if @hash.has_key?(word)
      true
    else 
      false
    end 
  end 
end 

I don't know what I'm doing wrong, but my tests keep failing and saying this:

> 1) Dictionary can check whether a given keyword exists
>      Failure/Error: @d.include?('fish').should be_false
>        expected false to respond to `false?`

I am confused at the error since it seems to be giving the correct answer. I would really appreciate if someone could take a few minutes to tell me what's wrong with my code. Thank you tons.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you browse the RSpec Expectations 2.99 and RSpec Expectations 2.14 and search the section - Truthiness and existentialism, you will find

expect(actual).to be_true  # passes if actual is truthy (not nil or false)
expect(actual).to be_false # passes if actual is falsy (nil or false)
# ...............
# ...

But of you browse RSpec Expectations 3.0 , the above method names got changed to -

expect(actual).to be_truthy    # passes if actual is truthy (not nil or false)
expect(actual).to be true      # passes if actual == true
expect(actual).to be_falsey    # passes if actual is falsy (nil or false)
# ...........
#......

It seems you are in 3.0, and using the method which were exist prior to this version. Thus you were getting the error.

I put the code in my test.rb file as below :-

class Dictionary
  def initialize
    @hash = {}
  end 

  def add(new_entry)
    new_entry.class == String ? @hash[new_entry] = nil : new_entry.each { |noun, definition| @hash[noun] = definition}    
  end 

  def entries
    @hash 
  end 

  def keywords
    @hash.keys
  end

  def include?(word)
    if @hash.has_key?(word)
      true
    else 
      false
    end 
  end 
end

And my spec/test_spec.rb file is -

require_relative "../test.rb"

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
end

Now I am running the code from my console, and it works :

arup@linux-wzza:~/Ruby> rspec -v
2.14.8
arup@linux-wzza:~/Ruby> rspec spec
.

Finished in 0.00169 seconds
1 example, 0 failures

Now I am changing the code in my spec/test_spec.rb file :-

require_relative "../test.rb"

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

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

and again run the test :-

arup@linux-wzza:~/Ruby> rspec -v
2.14.8
arup@linux-wzza:~/Ruby> rspec spec
F

Failures:

  1) Dictionary can check whether a given keyword exists
     Failure/Error: @d.include?('fish').should be_falsey
     NoMethodError:
       undefined method `falsey?' for false:FalseClass
     # ./spec/test_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.00179 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/test_spec.rb:8 # Dictionary can check whether a given keyword exists
arup@linux-wzza:~/Ruby>

Now, they also mentioned in the 3.0.0.beta1 / 2013-11-07 changelog

Rename be_true and be_false to be_truthy and be_falsey. (Sam Phippen)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...