Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

cannot test a controller action for a route defined with match

#5651

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.

Reported by EmmanuelOga · September 18th, 2010 @ 01:14 AM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. EmmanuelOga
    EmmanuelOga

    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
    

    September 18th, 2010 @ 01:17 AM

  2. Andrew White
    Andrew White
    • 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.

    September 18th, 2010 @ 07:53 AM

  3. Repository
  4. Repository
  5. Repository
  6. Repository
  7. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 12:09 PM

  8. Jeremy Kemper
  9. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 16th, 2010 @ 02:28 AM