Reputation: 1559
I am following a tutorial of Rails and I have a few pages that I am adding some tests for.
I am trying to use help_path instead of :help in my pages_controller_test :
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | #{@base_title}"
end
I added this line in my routes.rb file :
get '/help', to: 'pages#help'
But I get this error :
1) Error: PagesControllerTest#test_should_get_help: ActionController::UrlGenerationError: No route matches {:action=>"/help", :controller=>"pages"} test/controllers/pages_controller_test.rb:62:in `block in '
I have tried a few solutions but none of them solved my issue. I've also tried using this line instead :
match '/home' => 'main_pages#home', :as => :home
But it didn't work either.
My rails version is : 4.2.4 My Ruby version is : ruby 2.1.9p490 (2016-03-30 revision 54437) [x86_64-linux-gnu]
Output of $rake routes :
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
help GET /help(.:format) pages#help
courses GET /courses(.:format) pages#courses
programs GET /programs(.:format) pages#programs
schools GET /schools(.:format) pages#schools
dashboard GET /dashboard(.:format) pages#dashboard
profile GET /profile(.:format) pages#profile
account GET /account(.:format) pages#account
signout GET /signout(.:format) pages#signout
EDIT : I can use help_path .. etc in my html code without any issue, but in the test it gives that error. Thank you :)
Upvotes: 3
Views: 611
Reputation: 33420
I've ran your tests using the repo, Rails 4.2.4, Minitest 5.10.2 and the only one test that doesn't pass is the one using get help_path
. I put only the 3 tests in order to shorten the post:
PagesControllerTest#test_should_get_help_using_"get_:help" = 0.39 s = .
PagesControllerTest#test_should_get_help_using_"get_help_path" = 0.00 s = E
PagesControllerTest#test_should_get_help_using_"get_'help'" = 0.01 s = .
Finished in 0.405330s, 7.4014 runs/s, 9.8685 assertions/s.
1) Error:
PagesControllerTest#test_should_get_help_using_"get_help_path":
ActionController::UrlGenerationError: No route matches {:action=>"/help", :controller=>"pages"}
test/controllers/pages_controller_test.rb:70:in `block in <class:PagesControllerTest>'
3 runs, 4 assertions, 0 failures, 1 errors, 0 skips
What I did:
$ rm -rf Gemfile.lock (because of a json -v 1.8.1 gem error)
$ bundle
$ rake db:migrate
$ rake db:migrate RAILS_ENV=test
$ rake test test/controllers/pages_controller_test.rb -v
What you can use to make the test work with help_path
as the route to test specifying the controller and action is to use:
assert_routing
asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
Or:
assert_recognizes
asserts that path and options match both ways; in other words, it verifies that path generates options and then that options generates path. This essentially combines assert_recognizes and assert_generates into one step.
Like:
test "should get help using assert_recognizes and help_path" do
assert_recognizes({controller: 'pages', action: 'help'}, help_path)
assert_response :success
end
test "should get help using assert_routing and help_path" do
assert_routing help_path, controller: 'pages', action: 'help'
assert_response :success
end
Upvotes: 1