質問: rspecのshould_raiseをあらゆる種類の例外で使用する方法は? 私はこのようなことをしたいです:some_method.should_raise <any kind of exception, i don't care> どうすればいいですか?some_method.should_raise exception ...動作しません。
回答: rspec 2expect { some_method }.to raise_error expect { some_method }.to raise_error(someerror) expect { some_method }.to raise_error("oops") expect { some_method }.to raise_error(/oops/) expect { some_method }.to raise_error(someerror, "oops") expect { some_method }.to raise_error(someerror, /oops/) expect { some_method }.to raise_error(...){|e| expect(e.data).to eq "oops" } # rspec also offers to_not: expect { some_method }.to_not raise_error ... 注:raise_errorとraise_exceptionは交換可能です。rspec 1lambda { some_method }.should raise_error lambda { some_method }.should raise_error(someerror) lambda { some_method }.should raise_error(someerror, "oops") lambda { some_method }.should raise_error(someerror, /oops/) lambda { some_method }.should raise_error(...){|e| e.data.should == "oops" } # rspec also offers should_not: lambda { some_method }.should_not raise_error ... 注:raise_errorはraise_exceptionのエイリアスです。ドキュメント: https://www.relishapp.com/rspec rspec 2: https://www.relishapp.com/rspec/rspec-expectations/v/2-13/docs/built-in-matchers/raise-error-matcher rspec 1: http://apidock.com/rspec/spec/matchers/raise_error http://apidock.com/rspec/spec/matchers/raise_exception