Ruby on rails 检查第500页的测试用例&;404
我正在努力通过404和500页的测试用例。但是我有很多问题Ruby on rails 检查第500页的测试用例&;404,ruby-on-rails,ruby,ruby-on-rails-4,rspec,rspec3,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,Rspec3,我正在努力通过404和500页的测试用例。但是我有很多问题 1) 首先,我在app/views/errors/中有一个500.html.erb页面,没有被调用 2) 如果我运行以下测试,我的系统将冻结,需要重新启动系统 3) 如果我注释这一行请期待{get”/errors/foo}。引发_异常(ActionController::RoutingError)。因此,在我的控制器操作名页面中,500作为参数传递,但我的系统仍然冻结 有人能帮我解决这个问题吗 错误\u spec.rb require
1) 首先,我在app/views/errors/中有一个500.html.erb页面,没有被调用
2) 如果我运行以下测试,我的系统将冻结,需要重新启动系统
3) 如果我注释这一行请期待{get”/errors/foo}。引发_异常(ActionController::RoutingError)。因此,在我的控制器操作名页面中,500作为参数传递,但我的系统仍然冻结
有人能帮我解决这个问题吗 错误\u 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
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
错误\u controller.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
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
类错误控制器
在您的代码中,当调用/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
类错误控制器
这是一个仅测试的问题,即当您在浏览器中打开URL时,它是否工作?当我尝试从浏览器中点击时,它工作正常。您可以在测试中打开任何其他URL而不冻结系统吗,也就是说,问题是否特定于错误控制器
?是的,我可以在错误控制器中打开除404和404之外的其他URL500@Stefan-你知道我哪里做错了吗?