Narfanator
Narfanator

Reputation: 5813

Rails custom route not found when testing, but works for curl

In config/routes.rb:

match 'app_config/:version' => "application#appconfig"

test/functional/application_controller_test.rb:

require 'test_helper'
class ApplicationControllerTest < ActionController::TestCase
  test "app config" do
    get "/app_config/v2"
    assert_response :success
  end
end

rake test:

  1) Error:
test_app_config(ApplicationControllerTest):
ActionController::RoutingError: No route matches {:controller=>"application", :action=>"/app_config/v2"}

but $ curl localhost:3000/app_config/v2 works, and returns the response I expect, and rake routes shows the route as expected. Any idea what's going on, or how to investigate further? This is basically the only code in the project.

Upvotes: 0

Views: 415

Answers (1)

Andrew Hubbs
Andrew Hubbs

Reputation: 9436

The get method does not take a route. It takes an action name and a parameter's hash.

If you want to test the action, your tests should look like this:

test "for success" do
  get :appconfig, :version => "v2"
  assert_response :success
end

If you want to test routing there is a tutorial here in the documentation.

test "should route to appconfig" do
  assert_routing '/app_config/v2', { :controller => "application", :action => "appconfig", :version => "v2" }
end

Upvotes: 1

Related Questions