我有一个基于padrinorb的应用程序,我正在使用shoulda测试库。我需要一种方法来测试抛出异常。我尝试找到相同的文档,但找不到任何东西。
以下是我要测试的示例代码
def some_method(param)
raise APIException.new('Exception) if param == 2
end
如何在传递参数2时测试some_method是否抛出异常。
答案 0 :(得分:3)
expect{ some_method(params) }.to raise_error(APIException)
如果你使用TestUnit,这应该有效(source)
assert_raises(APIException) { some_method(params) }
要测试异常消息,请使用以下命令:
exception = assert_raises(Exception) { whatever.merge }
assert_equal( "message", exception.message )