RSpec - Testing Strong Parameters ActionController::ParameterMissing

喜欢而已 提交于 2019-12-08 03:15:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16724561/rspec-testing-strong-parameters-actioncontrollerparametermissing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!