Forum: Ruby Raise Error Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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 {
      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 topic is locked and can not be replied to.