我正在尝试通过404和404页的测试用例。 500.但是我遇到了很多问题
1)首先,我在app / views / errors /中有一个页面500.html.erb,它没有被调用。
2)如果我运行以下测试我的系统冻结,我需要重新启动我的系统
3)如果我将此行 expect {get" / errors / foo"}。评论为raise_exception(ActionController :: RoutingError)。所以在我的控制器中,动作名称页面 500作为参数传递,但我的系统仍然冻结了
任何人都可以帮我解决这个问题
errors_spec.rb
require "spec_helper"
describe "Errors" do
before do
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production"))
end
it "renders the 500 page" do
get "/errors/500"
expect(response.status).to eq(500)
end
it "renders the 404 page" do
get "/errors/404"
expect(response.status).to eq(404)
end
it "raises an exception if the page doesn't exist" do
expect {get "/errors/foo"}.to raise_exception(ActionController::RoutingError)
end
end
errors_controller.rb
class ErrorsController < ApplicationController
skip_before_filter :authenticate_user!
EXTERNAL_ERRORS = ['sso']
VALID_ERRORS = ['404', '403', '500', 'maintenance'] + EXTERNAL_ERRORS
def show
status = external_error? ? 500 : 200
render page , status: status
end
def blocked
end
private
def page
if VALID_ERRORS.include?(params[:id])
params[:id]
else
raise(ActionController::RoutingError.new("/errors/#{params[:id]} not found"))
end
end
def external_error?
EXTERNAL_ERRORS.include?(params[:id])
end
end
答案 0 :(得分:0)
在您的代码中,当调用/ errors / 500时,您将设置状态200。
def show
# external_error? returns false.
status = external_error? ? 500 : 200
render page , status: status # Status is 200.
end
使用pry
或byebug
等调试程序来检查状态。您的测试用例没有任何问题。试试这个。
class ErrorsController < ApplicationController
skip_before_filter :authenticate_user!
EXTERNAL_ERRORS = ['sso']
VALID_ERRORS = ['404', '403', '500', 'maintenance'] + EXTERNAL_ERRORS
def show
status = error_500? ? 500 : 200
render page , status: status
end
def blocked
end
private
def page
if VALID_ERRORS.include?(params[:id])
params[:id]
else
raise(ActionController::RoutingError.new("/errors/#{params[:id]} not found"))
end
end
def external_error?
EXTERNAL_ERRORS.include?(params[:id])
end
def error_500?
['500'].include?(params[:id]) || external_error?
end
end