Forum: Ruby Raise Error Question

42c86b1256b1c7fa498b7bf72041d4fd?d=identicon&s=25 Anthony Chang (royk)
on 2014-06-27 01:27
Attachment: rpn_calculator.rb (1 KB)
Attachment: rpn_calculator_spec.rb (4 KB)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.