0

I have an application based on padrinorb and I am using the shoulda testing library for the same. There is a method that I need to test throws exception. I tried finding the documentation for the same, but couldn't find anything.

Here's the sample code that I want to test

def some_method(param)
    raise APIException.new('Exception) if param == 2
end

How should I test that the some_method throws an exception when passed the parameter 2.

Ankit Dhingra
  • 6,534
  • 6
  • 31
  • 34

1 Answers1

3
expect{ some_method(params) }.to raise_error(APIException)

if you use TestUnit this should work (source)

assert_raises(APIException) { some_method(params) }

To test the exception message as well use the following:

exception = assert_raises(Exception) { whatever.merge }
assert_equal( "message", exception.message )
Community
  • 1
  • 1
gotva
  • 5,919
  • 2
  • 25
  • 35