如何测试某个操作是否引发ActionController::ParameterMissing异常?
例如:
it "raises an exception" do
post :create, {}
expect(response).to raise ActionController::ParameterMissing
end
以上似乎不起作用,它会通过ActionController::ParameterMissing异常测试失败。
如何测试某个操作是否引发ActionController::ParameterMissing异常?
例如:
it "raises an exception" do
post :create, {}
expect(response).to raise ActionController::ParameterMissing
end
以上似乎不起作用,它会通过ActionController::ParameterMissing异常测试失败。
使用raise_error匹配器使用期望块语法:
it "raises an exception" do
expect{ post(:create, {}) }.to raise_error ActionController::ParameterMissing
end
您的代码不起作用的原因是post(:create, {})引发异常。这发生在expect(response).to ...代码执行之前。由于#post消息不在begin...end块中,因此引发的异常被传递给 RSpec 失败的测试。