Search code examples
ruby-on-railsruby-on-rails-3testingroutesfunctional-testing

Why doesn't get '/foo' work in functional test in Rails 3?


Looking at the Rails Guides http://guides.rubyonrails.org/testing.html#integration-testing-examples and this SO question Rails Functional Test of Arbitrary or Custom URLs, the following should work in a test:

require 'test_helper'
class ApplicationControllerTest < ActionController::TestCase
  test "test authentication" do
    get "/dash"
    assert_response :success
  end
end

But I get an error: ActionController::RoutingError: No route matches {:controller=>"application", :action=>"/dash"}

My route is set up and works at the following URL: http://localhost:3000/dash

This is the route:

dash     /dash(.:format)   {:action=>"population", :controller=>"dashboard"}

Why wouldn't a get with a URL work?

I am running this test from class ApplicationControllerTest < ActionController::TestCase, which is different from dashboard controller. Do you have to run it from the functional test of the same name as the controller?

Working in Rails 3.07 using Test::Unit.


Solution

  • Yes, you do have to run it from the functional test for that particular controller. Rails is trying to be helpful, so it automatically picks what controller it sends the request to based on the test class's name.

    You'll probably also need to heed Marian's advice and use the action name rather than the URL:

    get :population
    

    And note that the get / post / etc. helper functions behave completely differently in integration tests (I believe your code would be correct there) - that's probably where you're getting hung up. I find this inconsistency irritating as well...

    Hope that helps!