![](/img/trans.png)
[英]Test for HTTP status code in some RSpec rails request exampes, but for raised exception in others
[英]rails 4 api rspec test http status code 410
我正在嘗試建立正確的rspec測試以驗證我的410狀態代碼處理程序是否正常運行。 這是路線捕獲全部的樣子:
match '*not_found', to: 'error#error_404', via: :all
我簡單的錯誤控制器:
class ErrorController < ApplicationController
def error_404
head status: 410
end
end
我當前的rspec:
require 'spec_helper'
describe ErrorController do
context "Method #error_404 handling missing routes =>" do
it "Should have the 410 status code.." do
get :error_404
expect(response.status).to be(410)
end
end
end
Rspec錯誤消息:
1) ErrorController Method #error_404 handling missing routes => Should have the 410 status code..
Failure/Error: get :error_404
ActionController::UrlGenerationError:
No route matches {:action=>"error_404", :controller=>"error"}
# ./spec/controllers/error_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
關於如何通過此測試的任何想法? 我知道這條路線將不存在,但我無法嘗試使用get
使其正常工作...
我想知道是否有人在這里有更好的主意.....但是,這是我如何解決的方法:
首先,我安裝了水豚 。
然后,我將路線調整為更具描述性:
match '*not_found', to: 'error#error_status_410', via: :all
然后我調整了錯誤控制器:
class ErrorController < ApplicationController
def error_status_410
head status: 410
end
end
最后,我調整了我的錯誤控制器規格:
require 'spec_helper'
describe ErrorController do
context "Method #error_status_410 handling missing routes =>" do
it "Should have the 410 status code.." do
visit '/will-never-be-a-route'
page.status_code == 410
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.