user3355098
user3355098

Reputation: 183

Rails test can't find route when it clearly exists?

Okay, so I am tired, and new to rails so possibly am missing something super basic. Anyways, I just started a new project and am implementing a simply controller for static pages. Wrote some unit tests just to make sure all my routes are correct (there are only 4 so far). Three of them pass but the fourth gives me this error message:

  1) Error:
StaticPagesControllerTest#test_terms_of_service_should_return_success:
ActionController::UrlGenerationError: No route matches {:action=>"terms", :controller=>"static_pages
"}
    test/controllers/static_pages_controller_test.rb:18:in `block in <class:StaticPagesControllerTes
t>'

Its saying it can't find the route. However, when I rake routes it clearly exists:

  about GET  /about(.:format)   static_pages#about
contact GET  /contact(.:format) static_pages#contact
privacy GET  /privacy(.:format) static_pages#privacy
  terms GET  /terms(.:format)   static_pages#terms_of_service

Here is that part of routes.rb:

  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'privacy' => 'static_pages#privacy'
  get 'terms' => 'static_pages#terms_of_service'

and here is the code for the test that is failing:

 test "terms_of_service should return success" do
    get 'terms'
    assert_response :success
    assert_select 'title', "Terms Of Service"
 end

I can visit localhost:3000/terms directly in the browser and it works. Any idea what is going on here?

Upvotes: 3

Views: 607

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

Controller tests don't test routes, the test actions. You want:

get 'terms_of_service'

Upvotes: 5

Related Questions