2017-06-07 29 views
3

我正在關注Rails教程,並且我有幾個頁面正在添加一些測試。_path的用法給出了「沒有路由匹配..」錯誤

我想使用help_path代替:幫助我pages_controller_test:

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

我說在我的routes.rb文件中這一行:

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

但我得到這個錯誤:

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 '

我已經嘗試了一些解決方案,但沒有一個解決了我的問題。 我也用這個行,而不是嘗試:

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

但它也不能工作。

我的Rails的版本是:4.2.4 我的紅寶石版本是:紅寶石2.1.9p490(2016年3月30日修訂54437)x86_64的-Linux的GNU]

輸出$耙路線:

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 

編輯: 我可以在我的html代碼中使用help_path ..等沒有任何問題,但在測試中,它給出了該錯誤。 謝謝:)

+0

不要在你的路由定義斜槓(使用' 'help''和'' home''代替)。另外,運行'rake routes'查看所有路線。 – mmichael

+0

你可以添加你的控制器和你的整個'routes.rb'嗎? –

+0

@SebastiánPalma我的application_controller只有一個hello函數 –

回答

1

我已經用repo,Rails 4.2.4,Minitest 5.10.2運行了你的測試,唯一一個不通過的測試是使用get help_path的測試。我只放了3次試驗,以縮短後:

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 

我做了什麼:

$ 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 

您可以用什麼來讓測試工作,help_path作爲路由測試指定控制器和行動是使用:

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.

或者:

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.

像:

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 
+0

非常感謝! –

+0

不客氣(: –

相關問題