2012-12-05 80 views
0

我想要做类似的方法调用测试:使用RSpec Rails的测试异常

call_to_method_1 param1 

这种方法可以提高例如:

raise msg1 if ... 
raise msg2 if ... 

我的问题是如何与测试例外的msg1,msg2与RSpec的异常消息。

回答

2
describe SomeClass do 
    let(:some_object) { described_class.new } 

    it 'should raise an exception' do 
    some_object.some_method('param').should raise_error(ExceptionClass, "exception_message") 
    end 
end 

如果raise "message"那么ExceptionClass将是RuntimeClass实例。

IMO最好提出特定类型的异常,而不是依靠消息。

+0

谢谢。它将raise信息传递给raise_error的最后一个参数。不过,我已经编辑了你的答案,完全符合要求。 –