Rspec/Rails controller testing 404 not working
rspec, ruby-on-rails
Solution
Rails is throwing an ActiveRecord::RecordNotFound error instead of redirecting to a general 404 page. You need to handle that error with a rescue_from in the controller and redirect to a 404 view with status 404.
Problem
Here is my test: ``` describe "GET show" do it "assigns service_request as @service_request" do get :show, { company_id: @company.id, id: service_request.id } expect(assigns(:service_request)).to eq service_request end it "returns 404 when service_request is not found" do get :show, { company_id: @company.id, id: "foo" } expect(response.status).to eq 404 end end ``` The error in my terminal is: ``` 1) ServiceRequestsController GET show returns 404 when service_request is not found Failure/Error: get :show, { company_id: @company.id, id: "foo" } ActiveRecord::RecordNotFound: Couldn't find ServiceRequest with 'id'=foo [WHERE (company_id IS NOT NULL)] # ./spec/controllers/service_requests_controller_spec.rb:44:in `block (3 levels) in <top (required)>' # -e:1:in `<main>' ``` Obviously this isn't correct, but I'm not sure what's wrong