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.

有帮助吗?

解决方案

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 )
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top