这是我对章节 Controller 的测试
require 'test_helper'
class ChaptersControllerTest < ActionController::TestCase
test "should get index_nklm" do
get :index_nklm
assert_response :success
end
test "should get show_nklm" do
get show_chapter_nklm_path(chapters(:one))
assert_response :success
end
end
这是我的routes.rb
get 'chapters_nklm' => 'chapters#index_nklm', as: :chapters_nklm
get 'chapter_nklm/:id/show' => 'chapters#show_nklm', as: :show_chapter_nklm
这是我的 chapters.yml 夹具:
one:
id: 12
chapter_name: MyString
chapter: MyString
description: MyText
display: 1
subject_id: 1
这是 chapters_controller.rb 的(部分):
def show_nklm
@chapter = Chapter.find(params[:id])
end
我不知道为什么会收到此错误消息?
Error:
ChaptersControllerTest#test_should_get_show_nklm:
ActionController::UrlGenerationError: No route matches {:action=>"/chapter_nklm/12/show", :controller=>"chapters"}
test/controllers/chapters_controller_test.rb:11:in `block in <class:ChaptersControllerTest>'
但是当我将“/chapter_nklm/12/show”复制到浏览器中时,一切正常吗?我的错误在哪里,我没有理解哪个概念?
提前致谢!
最佳答案
get
正在等待接收作为符号对象的操作,您正在传递整个路由别名以及所需的对象。
尝试在你的路由中使用 params
选项:
test 'should get show_nklm' do
get :show_nklm, params: { id: chapters(:one).id }
assert_response :success
end
关于ruby-on-rails - ActionController::UrlGenerationError:没有路由匹配 - 我的错误在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49967235/