【问题标题】:Why RSpec fails with TypeError when an expectation does't match?当期望不匹配时,为什么 RSpec 会因 TypeError 而失败?
【发布时间】:2016-08-14 01:35:59
【问题描述】:

当我运行rspec 并运行一个将通过的示例时,一切都很好。 但是当期望值不匹配时,我收到了一个奇怪的TypeError,而不是关于期望值和实际值之间差异的有用消息。

给定

  • 红宝石 2.3.1p112
  • rspec 3.5.2
  • rspec —init 生成的规范助手

何时

describe "Basics" do
  it "runs rspec successfully" do
    x = true
    expect(x).to be(true)
  end
end

那么一切都好

Finished in 0.00286 seconds (files took 0.09671 seconds to load)
1 example, 0 failures

但是当我将 x 更改为 false 时,匹配失败

何时

describe "Basics" do
  it "runs rspec successfully" do
    x = false
    expect(x).to be(true)
  end
end

那么

Failures:

  1) Basics runs rspec successfully
     Failure/Error: expect(x).to be(true)

     TypeError:   <——— 
       superclass must be a Class (Module given)
     # ./spec/lib/basic_spec.rb:4:in `block (2 levels) in <top (required)>'

Finished in 0.06168 seconds (files took 0.09715 seconds to load)
1 example, 1 failure

我希望像这样的输出

  lhs: false
  rhs: true

而不是这个 TypeError。

这里有什么问题?

【问题讨论】:

  • 我认为您只能使用 expect(x).to beexpect(x).not_to be 在所有其他情况下,您可以使用类似于 expect(x).to be &lt; 3 的运算符。
  • 还有这些构造,用于测试真/假而不是真/假:expect(x).to be_truthy; expect(x).to be_falsey

标签: ruby rspec


【解决方案1】:

原因是我的 gem 文件中的以下行

gem "prettyprint"

我从 Gemfile 中删除了它,一切都按预期工作。 正如我所知道的,可以在不使用任何 gem 的情况下使用 pp some_object

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-12
    • 2014-11-06
    • 1970-01-01
    • 2014-05-13
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多