【问题标题】:Trying to raise ArgumentError rspec试图提高 ArgumentError rspec
【发布时间】:2013-11-16 15:31:31
【问题描述】:

我有一个 Rspec 测试没有通过,我不明白为什么。所有其他测试都通过了,除了这个需要ArgumentError

测试看起来像这样:

describe "#evaluate" do
    it "raises error" do
        expect(rpn.evaluate('%')).to raise_error(ArgumentError)
    end
end

我的文件已设置好,因此会引发错误(作为 else 语句)

else
  raise ArgumentError.new
end

但是 rspec 告诉我这个

Failure/Error: expect(rpn.evaluate('%')).to raise_error(ArgumentError)
     ArgumentError:
       ArgumentError

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    做:

    expect { rpn.evaluate('%') }.to raise_error(ArgumentError)
    

    即通过一个block来期待

    【讨论】:

    • 就是这样。完美的。我将不得不阅读传递块的不同之处。
    【解决方案2】:

    将 rpn.evaluate('%') 作为参数传递和作为块传递的区别在于

    expect(rpn.evaluate('%')).to raise_error(ArgumentError)
    

    将检查 rpn.evaluate('%') 的 输出 是否引发 ArgumentError,而

    expect { rpn.evaluate('%') }.to raise_error(ArgumentError)
    

    将检查运行代码块 rpn.evaluate('%') 是否引发 ArgumentError。

    【讨论】:

      猜你喜欢
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2015-06-23
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多