Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Alternative fix for #573

Rather than starting with a fresh routeset, we take a copy of the
existing routes.
  • Loading branch information...
commit 44a60049be79cfbe9630a6a0aeeb222e34985586 1 parent 9f463b9
Andrew Donaldson authored
36 features/controller_specs/anonymous_controller.feature
View
@@ -383,3 +383,39 @@ Feature: anonymous controller
"""
When I run `rspec spec`
Then the examples should all pass
+
+ Scenario: redirecting via a before_filter with a named route in ApplicationController
+ Given a file named "spec/controllers/application_controller_spec.rb" with:
+ """ruby
+ require "spec_helper"
+
+ Rails.application.routes.draw do
+ root :to => "sessions#new"
+ end
+
+ class ApplicationController < ActionController::Base
+ private
+
+ def redirect_to_root
+ redirect_to(root_path)
+ end
+ end
+
+ describe ApplicationController do
+ controller do
+ before_filter :redirect_to_root, :only => :show
+
+ def show
+ end
+ end
+
+ context "GET #show" do
+ it "redirects to the root" do
+ get :show, :id => 1
+ expect(response).to redirect_to('/')
+ end
+ end
+ end
+ """
+ When I run `rspec spec`
+ Then the examples should all pass
2  lib/rspec/rails/example/controller_example_group.rb
View
@@ -64,7 +64,7 @@ def self.name; "AnonymousController"; end
metadata[:example_group][:described_class].class_eval(&body)
before do
- @orig_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
+ @orig_routes, @routes = @routes, @routes.dup
@routes.draw { resources :anonymous }
routes = @routes

2 comments on commit 44a6004

Andrew Donaldson
Owner

This works great in the context of the application (the app can now use root_path as expected), but you can't use the named route within the spec -- you have to refer to it as '/'.

To get that working needs someone more familiar with the rspec-rails internals! (@alindeman? :))

Andrew Donaldson
Owner

I can use named routes in my specs if I include the Routes URL helpers. Perhaps this is a deliberate change in rspec-rails.

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
end
Please sign in to comment.
Something went wrong with that request. Please try again.