Forum: Ruby Raise Error Question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
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 {
    }.to raise_error("calculator is empty")"

and am having this failure:


  1) RPNCalculator fails informatively when there's not enough values
stacked away
     Failure/Error: expect {
       expected Exception with "calculator is empty" but nothing was
     # ./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

  def value
    return @value

  def push(num)
    @array << num

  def plus
    if @array.length >= 2
      @value += @array[-1] + @array[-2]
    elsif @array.length == 1
      @value += @array[-1]
      raise "calculator is empty"

Any help will be appreciated, thanks.
This topic is locked and can not be replied to.