문제

How can one test if a certain action raises an ActionController::ParameterMissing exception?

For example:

it "raises an exception" do
  post :create, {}
  expect(response).to raise ActionController::ParameterMissing
end

The above does not seem to work, it will fail the test with the ActionController::ParameterMissing exception.

도움이 되었습니까?

해결책

Use the expect block syntax using the raise_error matcher:

it "raises an exception" do
  expect{ post(:create, {}) }.to raise_error ActionController::ParameterMissing
end

The reason that your code doesn't work is that post(:create, {}) raises the exception. This happens before the expect(response).to ... code gets executes. Since the #post message is not in a begin...end block, the raised exception is passed up to RSpec failing the test.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top