Ruby on rails 调整嵌套路由的rspec路由测试

Ruby on rails 调整嵌套路由的rspec路由测试,ruby-on-rails,rspec,routes,Ruby On Rails,Rspec,Routes,在构建我的应用程序时,我生成了脚手架,创建了标准的Rspec测试。我想使用这些测试进行覆盖,但由于嵌套路由,它们似乎失败了: 当我运行测试时,这是它的反馈: Failures: 1) ListItemsController routing routes to #index Failure/Error: get("/list_items").should route_to("list_items#index") No route matches "/list_item

在构建我的应用程序时,我生成了脚手架,创建了标准的Rspec测试。我想使用这些测试进行覆盖,但由于嵌套路由,它们似乎失败了:

当我运行测试时,这是它的反馈:

Failures:

  1) ListItemsController routing routes to #index
     Failure/Error: get("/list_items").should route_to("list_items#index")
       No route matches "/list_items"
     # ./spec/routing/list_items_routing_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.25616 seconds
1 example, 1 failure
列出项目控制器规格rb:

describe ListItemsController do
  # This should return the minimal set of attributes required to create a valid
  # ListItem. As you add validations to ListItem, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) { { "list_id" => "1", "project_id" => "1"  } }

  # This should return the minimal set of values that should be in the session
  # in order to pass any filters (e.g. authentication) defined in
  # ListItemsController. Be sure to keep this updated too.
  let(:valid_session) { {} }

  describe "GET index" do
    it "assigns all list_items as @list_items" do
      list_item = ListItem.create! valid_attributes
      get :index, project_id: 2, {}, valid_session
      assigns(:list_items).should eq([list_item])
    end
  end
routes.rb:

  resources :projects do
    member do
      match "list_items"
    end
  end
注: -我尝试将rpec测试本身更改为包含一个项目id,但这没有帮助。 -我正在使用Factory Girl生成夹具(不确定这是否相关)


谢谢你的帮助

首先,运行
rake routes
查看存在哪些路由

根据您的路线,我希望您有一个
项目控制器
,该控制器有一个操作
列表项目
。此操作将在
/projects/:id/list\u items
下可用

现在我只能从理论上推测你到底想要什么,但我会猜

如果您希望
/projects/:project_id/list_items
路由到
list_items#index
,则必须将路由更改为:

resources :projects do
    resources :list_items
end
您可以通过运行
rake routes
来确认这一点

然后修复路由规范中的断言:

get("/projects/23/list_items").should route_to("list_items#index", :project_id => "23")
RSpec v2.14+预期更新

expect(:get => "/projects/23/list_items").to route_to("list_items#index", :project_id => "23")

首先,运行
rake routes
,查看存在哪些路由

根据您的路线,我希望您有一个
项目控制器
,该控制器有一个操作
列表项目
。此操作将在
/projects/:id/list\u items
下可用

现在我只能从理论上推测你到底想要什么,但我会猜

如果您希望
/projects/:project_id/list_items
路由到
list_items#index
,则必须将路由更改为:

resources :projects do
    resources :list_items
end
您可以通过运行
rake routes
来确认这一点

然后修复路由规范中的断言:

get("/projects/23/list_items").should route_to("list_items#index", :project_id => "23")
RSpec v2.14+预期更新

expect(:get => "/projects/23/list_items").to route_to("list_items#index", :project_id => "23")

嗨,Dave,我想我必须在我的路由文件中有“memberdo”才能正确地嵌套和访问这些文件(否则我会遇到路由失败)。是否有办法将此成员关系传递给Rspec?当前,当我运行您给我的断言时,它失败了:失败/错误:get(“/projects/23/list#items”)。应该将#u路由到(“list#items#index”,“project#id=>“23”),识别的选项“index”,“controller”=>“list#items”,“id”=>“23”}>与“23”,“controller”=>“list#items”,“action”=>“index”}>,差异:“23”,“id”=>“23”}>.看。这是你的密码。做你想做的事。通过成员路由,路由器将请求路由到
projects#list_items
,我想这不是您想要的。请参阅Rails指南,了解路由如何工作以及如何嵌套资源:我刚刚注意到表示id的数字必须是字符串格式。在我的例子中,@director.id.to_sHi Dave,我认为我必须在我的路由文件中有“member do”才能正确嵌套和访问这些文件(否则我会导致路由失败)。是否有办法将此成员关系传递给Rspec?当前,当我运行您给我的断言时,它失败了:失败/错误:get(“/projects/23/list#items”)。应该将#u路由到(“list#items#index”,“project#id=>“23”),识别的选项“index”,“controller”=>“list#items”,“id”=>“23”}>与“23”,“controller”=>“list#items”,“action”=>“index”}>,差异:“23”,“id”=>“23”}>.看。这是你的密码。做你想做的事。通过成员路由,路由器将请求路由到
projects#list_items
,我想这不是您想要的。请参阅Rails指南,了解路由如何工作以及如何嵌套资源:我刚刚注意到表示id的数字必须是字符串格式。就我而言,@director.id.to\s