Created
December 20, 2012 03:48
-
-
Save nkpart/4342816 to your computer and use it in GitHub Desktop.
This gist demonstrates a bug in the interaction of routing with controller tests in Rails 3.2.9 (also tested on 3.2.8) If you have a `match` or `get` route which uses `redirect` as its destination, then the route behaves like a catch all, allowing any controller/action combination. You can see that in the `rake routes` output below, which shows …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
def index | |
render :text => "Test message" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class PostsControllerTest < ActionController::TestCase | |
test "indexes" do | |
# This test should fail, as the index route hasn't been defined. | |
get(:index) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b /b(.:format) :controller#:action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WtfRoutes::Application.routes.draw do | |
# THIS IS THE MAGIC SAUCE. This route reports as `:controller#:action`, behaving as a catch all route in your tests | |
# The leading slash seems to be important | |
match '/b' => redirect("whatever") | |
# Can also use `get` instead of `match` here and see the same problem. | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb | |
new file mode 100644 | |
index 0000000..5aa8bb8 | |
--- /dev/null | |
+++ b/app/controllers/posts_controller.rb | |
@@ -0,0 +1,5 @@ | |
+class PostsController < ApplicationController | |
+ def index | |
+ render :text => "Test message" | |
+ end | |
+end | |
diff --git a/config/routes.rb b/config/routes.rb | |
index 1dbf33e..c9bbaa6 100644 | |
--- a/config/routes.rb | |
+++ b/config/routes.rb | |
@@ -1,2 +1,5 @@ | |
WtfRoutes::Application.routes.draw do | |
+ # THIS IS THE MAGIC SAUCE. This route reports as `:controller#:action`, behaving as a catch all route in your tests | |
+ # The leading slash seems to be important | |
+ match '/b' => redirect("whatever") | |
end | |
diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb | |
new file mode 100644 | |
index 0000000..d40b39b | |
--- /dev/null | |
+++ b/test/functional/posts_controller_test.rb | |
@@ -0,0 +1,7 @@ | |
+require 'test_helper' | |
+ | |
+class PostsControllerTest < ActionController::TestCase | |
+ test "indexes" do | |
+ get(:index) | |
+ end | |
+end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment