Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can you test the 'catch all route' using ordinary controller tests?
    primarykey
    data
    text
    <p><em>Note: As per RafaeldeF.Ferreira's suggestion, this question has been heavily edited since its original form.</em></p> <p>My JSON-based app needs to return something sensible when given a bad route. We already know that the following <code>rescue_from ActionController::RoutingError</code> doesn't work in Rails 3.1 and 3.2:</p> <pre><code># file: app/controllers/application_controller.rb class ApplicationController &lt; ActionController::Base protect_from_forgery rescue_from ActionController::RoutingError, :with =&gt; :not_found ... end </code></pre> <p>(This is well documented in <a href="https://github.com/rails/rails/issues/671" rel="noreferrer">https://github.com/rails/rails/issues/671</a>.) So I implemented what José Valim describes in <a href="http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/" rel="noreferrer">this blog entry (item 3)</a>, and details are provided below.</p> <p>But testing it has been problematic. This controller rspec test:</p> <pre><code># file: spec/controllers/errors_controller.rb require 'spec_helper' require 'status_codes' describe ErrorsController do it "returns not_found status" do get :not_found response.should be(StatusCodes::NOT_FOUND) end end </code></pre> <p>fails with:</p> <pre><code>ActionController::RoutingError: No route matches {:format=&gt;"json", :controller=&gt;"sites", :action=&gt;"update"} </code></pre> <p>Yet this integration test calls ErrorsController#not_found and succeeds:</p> <pre><code># file: spec/requests/errors_spec.rb require 'spec_helper' require 'status_codes' describe 'errors service' do before(:each) do @client = FactoryGirl.create(:client) end it "should catch routing error and return not_found" do get "/v1/clients/login.json?id=#{@client.handle}&amp;password=#{@client.password}" response.status.should be(StatusCodes::OK) post "/v1/sites/impossiblepaththatdoesnotexist" response.status.should be(StatusCodes::NOT_FOUND) end end </code></pre> <p>So: Is there a way to test the 'catch all route' using ordinary controller tests?</p> <h2>implementation details</h2> <p>If you want to see the implementation, here are the relevant code snippets</p> <pre><code># config/routes.rb MyApp::Application.routes.draw do ... all other routes above here. root :to =&gt; "pages#home" match "/404", :to =&gt; "errors#not_found" end # config/application.rb module MyApp class Application &lt; Rails::Application config.exceptions_app = self.routes ... end end # config/environments/test.rb MyApp::Application.configure do ... config.consider_all_requests_local = false ... end # app/controllers/errors_controller.rb class ErrorsController &lt; ApplicationController def not_found render :json =&gt; {:errors =&gt; ["Resource not found"]}, :status =&gt; :not_found end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload