This project is archived and is in readonly mode.
cannot test a controller action for a route defined with match
Reported by EmmanuelOga | September 18th, 2010 @ 01:14 AM | in 3.0.2
Apparently, there is no easy way to test a route defined using the router's match helper.
Using rails 3.0, I define a route using match to catch several different feeds
/feed/google_base.xml /feed/custom.xml
etc.. Here is the code.
# route
B0rken::Application.routes.draw do
match 'feed/:service(.xml)' => "/public/listings#feed", :format => :xml, :as => :feed
end
# controller
class Public::ListingsController < ApplicationController
def feed
render :xml => ["You just requested feed #{ params[:service] }"]
end
end
# test
class Public::ListingsControllerTest < ActionController::TestCase
test "should get a feed" do
get "feed", :service => "google_base", :format => "xml"
assert_response :success
end
end
Even though I can rackup this rails3 controller and retrieve the
feed w/o problem,
I cannot find a way to make the test retrieve the route.
I understand there might be better ways to define the route in
this specific case, but,
If a route is valid, and I'm able to match it in the live rails
app, and it returns the expected result,
then I think there should be away to test that controller action in
my app test suite.
Comments and changes to this ticket
-
EmmanuelOga September 18th, 2010 @ 01:17 AM
Forgot to add the test unit output, here it is:
▸ bundle show rails /home/emmanuel/.rvm/gems/ruby-1.8.7-p174/gems/rails-3.0.0esponse :success ▸ ruby -I test/ test/functional/public_listings_controller_test.rb Loaded suite test/functional/public_listings_controller_test Started E Finished in 0.113132 seconds. 1) Error: test_should_get_a_feed(Public::ListingsControllerTest): ActionController::RoutingError: No route matches {:action=>"feed", :format=>"xml", :controller=>"public/listings", :service=>"google_base"} test/functional/public_listings_controller_test.rb:6:in `test_should_get_a_feed' 1 tests, 0 assertions, 0 failures, 1 errors
-
Andrew White September 18th, 2010 @ 07:53 AM
- Milestone cleared.
- State changed from new to open
- Assigned user set to Andrew White
- Importance changed from to Low
The thing that's making your test fail is the leading slash on the controller spec - remove that and the test will pass. However your route could do with a little more tweaking - look at the output of
rake routes
:feed /feed/:service(.xml)(.:format) {:format=>:xml, :action=>"feed", :controller=>"public/listings"}
As you can see you have a format parameter and an optional .xml segment. I'm assuming that you want to enforce xml urls in which case you need to turn off the automatic format parameter and add in a default for it:
match "/feed/:service.xml" => "public/listings#feed", :format => false, :defaults => { :format => "xml" }, :as => :feed
which gives the following
rake routes
output:feed /feed/:service.xml {:format=>"xml", :action=>"feed", :controller=>"public/listings"}
I'll mark it as open because we probably want to clean up the controller automatically.
-
Repository September 18th, 2010 @ 01:33 PM
- State changed from open to resolved
(from [013ed1c0503e59dc8f1cfff1bd00d0e579c604a1]) Remove leading slash from controller [#5651 state:resolved] http://github.com/rails/rails/commit/013ed1c0503e59dc8f1cfff1bd00d0...
-
Repository September 18th, 2010 @ 01:48 PM
(from [301462c89b2f21f399ff538d821d09258836dafa]) Remove leading slash from controller [#5651 state:resolved] http://github.com/rails/rails/commit/301462c89b2f21f399ff538d821d09...
-
Repository September 18th, 2010 @ 06:00 PM
(from [0bffd7d933a46df958c3e6bae1d9871aca515a12]) Raise ArgumentError instead of normalizing controller name when there is a leading slash [#5651 state:resolved] http://github.com/rails/rails/commit/0bffd7d933a46df958c3e6bae1d987...
-
Repository September 18th, 2010 @ 06:00 PM
(from [b79a782a05df50028c04b4336d45875352e89d12]) Raise ArgumentError instead of normalizing controller name when there is a leading slash [#5651 state:resolved] http://github.com/rails/rails/commit/b79a782a05df50028c04b4336d4587...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Referenced by
- 5651 cannot test a controller action for a route defined with match (from [013ed1c0503e59dc8f1cfff1bd00d0e579c604a1]) Remove ...
- 5651 cannot test a controller action for a route defined with match (from [301462c89b2f21f399ff538d821d09258836dafa]) Remove ...
- 5651 cannot test a controller action for a route defined with match (from [0bffd7d933a46df958c3e6bae1d9871aca515a12]) Raise A...
- 5651 cannot test a controller action for a route defined with match (from [b79a782a05df50028c04b4336d45875352e89d12]) Raise A...