【问题标题】:rspec failing error: expected false to respond to `false?`rspec 失败错误:预期假以响应“假?”
【发布时间】:2014-07-19 05:16:14
【问题描述】:

我正在运行这部分测试:

 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

使用此代码:

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 

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

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

我对这个错误感到困惑,因为它似乎给出了正确的答案。如果有人能花几分钟告诉我我的代码有什么问题,我将不胜感激。 谢谢吨。

【问题讨论】:

    标签: ruby rspec rspec3


    【解决方案1】:

    如果您浏览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)
    # ...............
    # ...
    

    但是你们浏览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)
    # ...........
    #......
    

    您似乎在 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
    

    而我的 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
    

    现在我从控制台运行代码,它可以工作了:

    arup@linux-wzza:~/Ruby> rspec -v
    2.14.8
    arup@linux-wzza:~/Ruby> rspec spec
    .
    
    Finished in 0.00169 seconds
    1 example, 0 failures
    

    现在我正在更改 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
    

    然后再次运行测试:-

    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>
    

    现在,他们在3.0.0.beta1 / 2013-11-07 changelog中也提到了

    be_truebe_false 重命名为 be_truthybe_falsey。 (萨姆·菲彭)

    【讨论】:

    • 非常感谢您的详细解答!现在已修复 :) 我会投票支持您的答案,但我没有足够的声誉:/
    • @user3417583 没问题,,,享受 Rspec!
    • 这可能是一个稍微不同的问题,但我发现 v 2.99.0 这些被标记为弃用警告,所以可能是关于在你的 Gemfile 中某个地方的碰撞版本的教训。当然是给我的!
    • 我刚刚通过您出色的回答解决了另一个问题。真的很有帮助,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多