2

在构建我的应用程序时,我生成了创建标准 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

我如何告诉 Rspec 有嵌套路由?

以下是删减的文件:

list_items_routing_spec.rb:

require "spec_helper"

describe ListItemsController do
  describe "routing" do

    it "routes to #index" do
      get("/list_items").should route_to("list_items#index")
    end

end

list_items_controller_spec.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

路线.rb:

  resources :projects do
    member do
      match "list_items"
    end
  end

注意: - 我尝试将 rpec 测试本身更改为包含 project_id,但这并没有帮助。- 我正在使用 Factory Girl 生成夹具(不确定这是否相关)

谢谢你的帮助!

4

1 回答 1

4

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

根据您在路线中的内容,我希望您有一个ProjectsController具有 action 的内容list_items。此操作将在 下可用/projects/:id/list_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")
于 2013-07-15T19:27:51.183 回答