Using _path gives the error "No routes ..."

I am following the Rails tutorial and I have several pages to which I add some tests.

I'm trying to use help_path instead of: help in my_controller_test pages:

test "should get help" do
  get help_path
  assert_response :success
  assert_select "title", "Help | #{@base_title}"
end

      

I added this line to my routes.rb file:

 get  '/help',    to: 'pages#help'

      

But I am getting 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 the block in

I tried several solutions but none of them solved my problem. I also tried using this line:

match '/home' => 'main_pages#home', :as => :home

      

But that didn't work either.

My rails version: 4.2.4 My Ruby version: ruby ​​2.1.9p490 (version 2016-03-30 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 problem, but in test it gives this error. Thank:)

+3


source to share


1 answer


I ran tests using the repo, Rails 4.2.4, Minitest 5.10.2 and the only test that fails is the one using get help_path

. I only put 3 tests to shorten the message:

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've done:

$ 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 help_path

as a validation route defining a controller and an action is to use:



assert_routing

asserts that the routing of the given path was handled correctly and that the parsed parameters (given in the expected hash value) match the track. Basically, it claims that Rails recognizes the route specified by expected_options.

Or:

assert_recognizes

asserts that the path and parameters match both paths; in other words, it checks that the path generates parameters and then generates the parameters for the track. This essentially combines assert_recognizes and assert_generates in one step.

how

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

      

+1


source







All Articles