【问题标题】:Adjusting rspec routing tests for nested routes调整嵌套路由的 rspec 路由测试
【发布时间】:2013-07-13 15:38:18
【问题描述】:

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

routes.rb:

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

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

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails rspec routes


    【解决方案1】:

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

    根据您在路线中的内容,我希望您有一个ProjectsController,它有一个动作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")
    

    【讨论】:

    • 嗨,戴夫,我认为我必须在我的路由文件中包含“member do”才能正确嵌套和访问这些文件(否则我会遇到路由失败)。有没有办法将此成员关系传递给 Rspec?目前,当我运行您给我的断言时,它失败了:失败/错误:get("/projects/23/list_items").should route_to("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 指南:guides.rubyonrails.org/routing.html#nested-resources
    • 我刚刚注意到代表id的数字必须是字符串格式。就我而言,@director.id.to_s
    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多