gpt4 book ai didi

ruby-on-rails - _path 的使用给出了 "No route matches.."错误

转载 作者:数据小太阳 更新时间:2023-10-29 08:06:58 24 4
gpt4 key购买 nike

我正在学习 Rails 教程,我有几个页面要添加一些测试。

我正在尝试在我的 pages_controller_test 中使用 help_path 而不是 :help :

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我的 Ruby 版本是:ruby 2.1.9p490(2016-03-30 修订版 54437)[x86_64-linux-gnu]

$rake 路由的输出:

 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 .. 等等,没有任何问题,但在测试中它给出了那个错误。谢谢:)

最佳答案

我已经使用 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 作为测试指定 Controller 和操作的路径来使测试工作:

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

关于ruby-on-rails - _path 的使用给出了 "No route matches.."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44422516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com