嵌套控制器的RSpec RoutingError

时间:2011-08-15 17:16:36

标签: ruby-on-rails rspec

我正在测试嵌套控制器并收到以下错误:

  1) Checklists::ItemsController index action should render index template
     Failure/Error: get :index, :checklist_id => checklist.id
     ActionController::RoutingError:
       No route matches {:checklist_id=>1, :controller=>"checklists/items"}

在浏览器中加载/检查列表/ 1 /项目加载正常。

我在规范中遗漏了什么?

路线:

  resources :checklists do
    resources :items, :controller => "Checklists::Items"
  end

位于命名空间文件夹中的控制器( /app/controllers/checklists/items_controller.rb ):

class Checklists::ItemsController < ApplicationController
  respond_to :html, :json

  def index
    @checklist_items = @checklist.items
    respond_with @checklist_items
  end
end

规范( /spec/controllers/checklists/items_controller_spec.rb ):

describe Checklists::ItemsController do
  let(:user) { Factory :user, :role => 'admin' }
  let(:checklist) { Factory(:checklist) }
  let(:checklist_item) { Factory(:checklist_item) }

  before(:each) do
    sign_in_to(controller, user)
    Checklist.stub(:find => checklist)
  end

  it "index action should render index template" do
    get :index, :checklist_id => checklist.id
    response.should render_template(:index)
  end
end

更新:检查清单项目的路线

checklist_items GET    /checklists/:checklist_id/items(.:format) {:action=>"index", :controller=>"Checklists::Items"}
                POST   /checklists/:checklist_id/items(.:format) {:action=>"create", :controller=>"Checklists::Items"}
new_checklist_item GET    /checklists/:checklist_id/items/new(.:format) {:action=>"new", :controller=>"Checklists::Items"}
edit_checklist_item GET    /checklists/:checklist_id/items/:id/edit(.:format) {:action=>"edit", :controller=>"Checklists::Items"}
 checklist_item GET    /checklists/:checklist_id/items/:id(.:format) {:action=>"show", :controller=>"Checklists::Items"}
                PUT    /checklists/:checklist_id/items/:id(.:format) {:action=>"update", :controller=>"Checklists::Items"}
                DELETE /checklists/:checklist_id/items/:id(.:format) {:action=>"destroy", :controller=>"Checklists::Items"}

1 个答案:

答案 0 :(得分:2)

事实证明问题的解决方案是在路线中:

我改变了

resources :items, :controller => "Checklists::Items"

resources :items, :controller => "checklists/items"

现在可以使用了