I’ve recently started learning Rails by using it for a personal project. I’m running into an issue with a Controller test that was autogenerated (when I created the model, I think?) Here’s the test:
require "test_helper"
class DaysControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get days_index_url
assert_response :success
end
end
However, when I run the test, I get the following error:
Error:
DaysControllerTest#test_should_get_index:
NameError: undefined local variable or method `days_index_url' for an instance of DaysControllerTest
This is in spite of the fact that I have defined a days#index method and included resources :days in my routes.rb file.
All of my attempts to find an explanation for why this might be happening have come up short. Any help would be greatly appreciated!
If you run rails routes in your terminal, it should list all the valid route helpers you have. It seems that for some reason the route got misnamed into days_index_url where it should probably be called days_url.
(sending this again from the Web interface, because my e-mail reply got flagged as “machine-generated” by Discourse…)
If your controller is named day_controller.rb rather than days_controller.rb that’s a strong clue that it would need the index part in the route name. Check the actual model name of the controller. DayController vs DaysController. Which one do you have?
Thank you both for taking the time to help! @walterdavis The file name is days_controller.rb with the class name DaysController. Does that sound right to you?
@hakuninbin/rails routes -c Day gives the following output:
Prefix Verb URI Pattern Controller#Action
days GET /days(.:format) days#index
POST /days(.:format) days#create
new_day GET /days/new(.:format) days#new
edit_day GET /days/:id/edit(.:format) days#edit
day GET /days/:id(.:format) days#show
PATCH /days/:id(.:format) days#update
PUT /days/:id(.:format) days#update
DELETE /days/:id(.:format) days#destroy
search GET /search(.:format) days#index
root GET / days#index
Right, the output is telling you is that the helper to get the days#index route is days_url, not days_index_url. So your test should say:
require "test_helper"
class DaysControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get days_url # <== fix this line
assert_response :success
end
end