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:inblock (2 levels) in <top (required)>’
Changing the test to either of these two variants allows the the to
pass:
FYI, the same thing happens with rspec 2.0.0.beta.20 as well:
Service should raise an ArgumentError if currency_amount is nil
Failure/Error: lambda
{ @service.currency_to_dollars(nil) }.should
raise_error(ArgumentError)
expected ArgumentError, got #<NoMethodError: undefined method
`ArgumentError’ for #Service:0x000001029665d8>
./test/spec/service_spec.rb:92:in `block (2 levels) in <top
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>
This message is telling you there is no ArgumentError method, not that
the constant ArgumentError is missing. The method needs to be (adding
“.new”):
def currency_to_dollars(currency_amount)
raise ArgumentError.new("Currency amount can't be nil") if
currency_amount.nil?
end
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: