【发布时间】:2017-03-17 23:16:05
【问题描述】:
我正在尝试创建一个 RSpec 测试来检测请求是否会使控制器崩溃,通常是 500 错误。所以我希望能够区分:
nil.invalid_method # raises NoMethodError
来自
params.require(:required_parameter) # raises ActionController::ParameterMissing
以通用方式在控制器中。当我进行request、feature 或controller 测试时,它会引发异常:
describe "Post", type: :request do
it 'does not crash when no params given' do
post '/posts' # this line launches an exception
expect(page).to_not have_http_status(500)
end
end
似乎在 RSpec(或我不知道的 Rails)之前有不同的行为,类似于我正在寻找的:
- rails 4 api rspec test http status code 410
- Rspec shows different status code than browser
- How to use HTTP status code symbols in RSpec?
我该怎么做?或者你会怎么做?
感谢您的宝贵时间。
【问题讨论】:
-
@max @spickermann 我没有正确解释自己。我的意思不是检查引发了哪个异常。也许我的控制器正在以另一种方式处理问题,即:它在 html 中返回一条错误消息。因此,我想要一种通用方式 来检测不存在“编码错误”。 Rails 使用异常来处理几种情况,这些情况是正确的代码,例如
ActionController::ParameterMissing,因为我不能使用except { ... }.to_not raise_exception。
标签: ruby-on-rails ruby rspec rspec-rails