rspec失败错误:假设错误响应`false?`

use*_*583 26 ruby rspec rspec3

我正在运行这部分测试:

 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
Run Code Online (Sandbox Code Playgroud)

使用此代码:

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 
Run Code Online (Sandbox Code Playgroud)

我不知道我做错了什么,但我的测试仍然失败并说:

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

我对错误感到困惑,因为它似乎给出了正确的答案.如果有人花几分钟时间告诉我我的代码有什么问题,我真的很感激.谢谢你.

Aru*_*hit 72

如果您浏览RSpec Expectations 2.99RSpec Expectations 2.14并搜索部分 - 真实性和存在主义,您会发现

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)
# ...............
# ...
Run Code Online (Sandbox Code Playgroud)

但是你浏览RSpec Expectations 3.0,上面的方法名称改为 -

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)
# ...........
#......
Run Code Online (Sandbox Code Playgroud)

看起来你是3.0,并使用此版本之前存在的方法.因此你得到了错误.

我把代码放在我的test.rb文件中,如下所示: -

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
Run Code Online (Sandbox Code Playgroud)

我的spec/test_spec.rb文件是 -

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
Run Code Online (Sandbox Code Playgroud)

现在我从我的控制台运行代码,它的工作原理如下:

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

Finished in 0.00169 seconds
1 example, 0 failures
Run Code Online (Sandbox Code Playgroud)

现在我正在更改spec/test_spec.rb文件中的代码: -

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
Run Code Online (Sandbox Code Playgroud)

并再次运行测试: -

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>
Run Code Online (Sandbox Code Playgroud)

现在,他们还在3.0.0.beta1/2013-11-07更新日志中提到过

重命名be_truebe_false转到be_truthybe_falsey.(Sam Phippen)

  • 非常感谢您的详细解答!现在已经修好了:)我会投票给你答案,但我没有足够的声誉:/ (2认同)