基本上我正在尝试回答一系列Rspec指令。其中一条说明就是:
it "fails informatively when there's not enough values stacked away" do
expect {
calculator.plus
}.to raise_error("calculator is empty")
end
所以我了解了raise_error以及如何回答它,是我创建某种错误/异常。
def plus
@array_nums.length >= 2 ? @array_nums << @array_nums.pop + @array_nums.pop : raise {"calculator is empty"}
@value = @array_nums[-1]
要清楚底部有一个结尾但是文本编辑器没有处理它。所以我提出错误,我试图引发ArgumentError以及所有这些,但我不断从rspec获得这个响应:
Failure/Error:
expect {
calculator.plus
}.to raise_error("calculator is empty")
expected Exception with "calculator is empty", got RuntimeError with backtrace:
# ./lib/12_rpn_calculator.rb:16:in `plus'
# ./spec/12_rpn_calculator_spec.rb:119:in `block (3 levels) in <top (required)>'
# ./spec/12_rpn_calculator_spec.rb:118:in `block (2 levels) in <top (required)>'
# ./spec/12_rpn_calculator_spec.rb:118:in `block (2 levels) in <top (required)>'
我尝试的任何其他引发的错误都不起作用,我正在努力找到正确的方向来接受这个。也许我在寻找错误的地方?感谢任何帮助,谢谢!
答案 0 :(得分:0)
试试这个:
@array_nums.length >= 2 ? @array_nums << @array_nums.pop + @array_nums.pop : raise StandardError, 'calculator is empty'