[rspec-users] should raise_error(ArgumentError) resulting in NoMethodError

Brian Ploetz bploetz at gmail.com
Wed Aug 25 23:13:00 EDT 2010


Environment
------------------
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
rspec (1.3.0)

Code
-------
    def currency_to_dollars(currency_amount)
      raise ArgumentError("Currency amount can't be nil") if
currency_amount.nil?
    end

Spec
--------
    it "should raise an ArgumentError if currency_amount is nil" do
      lambda { @service.currency_to_dollars(nil) }.should
raise_error(ArgumentError)
    end

Results in this failure:
1)
'Service should raise an ArgumentError if currency_amount is nil' FAILED
expected ArgumentError, got #<NoMethodError: undefined method
`ArgumentError' for #<Service:0x0000010087e5f0>>
test/spec/service_spec.rb:92:in `block (2 levels) in <top (required)>'


Changing the test to either of these two variants allows the the to pass:

    lambda { @service.currency_to_dollars(nil) }.should
raise_error(StandardError)
    lambda { @service.currency_to_dollars(nil) }.should raise_error

Why am I unable to test for a specific StandardError subclass?

Thanks in advance for any help.
BP
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100825/4b96c5f4/attachment.html>


More information about the rspec-users mailing list