Raise Error Question

Attached are two files that are an RPN calculator and its accompanying
rspec tests, respectively.

I am having an issue with passing the test:

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

and am having this failure:

"Failures:

  1. RPNCalculator fails informatively when there’s not enough values
    stacked away
    Failure/Error: expect {
    expected Exception with “calculator is empty” but nothing was
    raised

    ./12_rpn_calculator/rpn_calculator_spec.rb:117:in `block (2

levels) in <top (required)>’"

Relevant code for this is as follows:

"class RPNCalculator
def initialize(value = 0, array = [])
@value = value
@array = array
end

def value
return @value
end

def push(num)
@array << num
end

def plus
if @array.length >= 2
@value += @array[-1] + @array[-2]
@array.pop(2)
elsif @array.length == 1
@value += @array[-1]
@array.pop
else
raise “calculator is empty”
end
end
end"

Any help will be appreciated, thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs