我想創建一個RSpec測試,它檢測請求是否可以使控制器崩潰,通常是500錯誤。所以我想能夠區分之間:從如何測試請求不會返回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',因爲我不能使用'除了{...} .to_not raise_exception'。 –