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 Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)