Read more

Rails 3/4: How to add routes for specs only

Arne Hartherz
October 07, 2013Software engineer at makandra GmbH

If you want to have routes that are only available in tests (e.g. for testing obscure redirects), you can use the with_routing helper -- but that one destroys existing routes which may break a specs that require them to work.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

To keep both "regular" and test routes, do this:

class MyApplicationController < ActionController::Base
  def show
    render text: 'Welcome to my application'
  end
end

test_routes = Proc.new do
  get '/my_application' => 'my_application#show'
end
Rails.application.routes.eval_block(test_routes)

Put it into a place like spec/support/test_routes.rb, or a similar place that's loaded automatically on a spec run.

Posted by Arne Hartherz to makandra dev (2013-10-07 16:32)