I am trying to build the correct rspec test to verify my 410 status code handler is working. Here is what the route catch all looks like:
match '*not_found', to: 'error#error_404', via: :all
My simple error controller:
class ErrorController < ApplicationController
def error_404
head status: 410
end
end
My current 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 Error Message:
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)>'
Any ideas on how to get this test to pass? I know the route will not exist but I am having trouble trying to use get
to make it work...
I wonder if someone has a better idea here..... but, here is how I solved it:
First I installed capybara .
Then I adjusted the route to be more descriptive:
match '*not_found', to: 'error#error_status_410', via: :all
Then I adjusted my error controller:
class ErrorController < ApplicationController
def error_status_410
head status: 410
end
end
Finally I adjusted my error controller spec:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.