2014-10-10 50 views
0

我似乎无法找到看起来像一个非常简单的谷歌搜索。我在的你如何编码解决rspec中的raise_error?

it "fails informatively when there's not enough values stacked away" do 
    expect { 
    calculator.plus 
    }.to raise_error("calculator is empty") 

我在一个RPN计算器程序我写的加方法的代码的程序有一个RSpec是:

def plus 
    if @calculator.length == 0 
     raise ArgumentError.new("calculator is empty") 
    end 
    sum = @calculator[-1] + @calculator[-2] 
    2.times {@calculator.pop} 
    @calculator.push(sum) 
end 

但它不能解决任何事情。我对编程非常陌生,特别是在例外方面,所以我们不胜感激。

我确定它非常简单,但我一直在这个问题上停留了一个多小时,无处不在。

这是当我运行包RSpec的错误消息我得到:

 
F  Failure/Error: expect { 
     expected Exception with "calculator is empty", got # with backtrace: 
     # ./lib/12_rpn_calculator.rb:22:in `minus' 
     # ./spec/12_rpn_calculator_spec.rb:124:in `block (3 levels) in ' 
     # ./spec/12_rpn_calculator_spec.rb:123:in `block (2 levels) in ' 
+0

目前还不清楚需要修复什么......您在看什么? – 2014-10-10 03:26:46

+0

我把原来的帖子中的失败,它张贴所有whacky虽然,我不想弄乱它是如何从命令提示符粘贴。 – 2014-10-10 03:33:03

+0

我想通了...叹了口气这么容易.. 摆脱了如果和只是 提高“计算器是空的”,如果@ calculator.length <= 1 – 2014-10-10 05:03:12

回答

-2

我写了下面。我有很多失败解决这个测试。

def times 
    [email protected] 
    [email protected] 
    if n1==nil || n2==nil 
    raise('calculator is empty') 
    else 
    @calculator.push(n2.to_f*n1.to_f) 
    end 
end