如何为嵌套路由的新方法驱动控制器规范?

时间:2014-08-01 15:48:49

标签: ruby-on-rails ruby rspec ruby-on-rails-3.2

我有一个相当简单的路线设置:

  resources :users, only: [:show] do
    resources :account_groups

生成这些路线:

$rake routes CONTROLLER=api/account_groups
    api_user_account_groups GET    /api/users/:user_id/account_groups(.:format)          api/account_groups#index
                            POST   /api/users/:user_id/account_groups(.:format)          api/account_groups#create
 new_api_user_account_group GET    /api/users/:user_id/account_groups/new(.:format)      api/account_groups#new
edit_api_user_account_group GET    /api/users/:user_id/account_groups/:id/edit(.:format) api/account_groups#edit
     api_user_account_group GET    /api/users/:user_id/account_groups/:id(.:format)      api/account_groups#show
                            PUT    /api/users/:user_id/account_groups/:id(.:format)      api/account_groups#update
                            DELETE /api/users/:user_id/account_groups/:id(.:format)      api/account_groups#destroy

当我尝试为controller#new编写规范时出现问题:

describe '#new' do
  it 'responds 200' do
    get :new
    response.status.should eq 200
  end
end

失败,因为路由需要您指定:user_id

1) Api::AccountGroupsController#new responds 200
     Failure/Error: get :new
     ActionController::RoutingError:
       No route matches {:controller=>"api/account_groups", :action=>"new"}

但是,当我指定:user_id时:

describe '#new' do
  it 'responds 200' do
    get :new, {user_id: user.id}
    response.status.should eq 200
  end
end

它抱怨说我添加了一个意想不到的论点:

1) Api::AccountGroupsController#new responds 200
     Failure/Error: get :new, {user_id: user.id}
     ArgumentError:
       wrong number of arguments (1 for 0)

知道如何使这项工作?谢谢!

1 个答案:

答案 0 :(得分:0)

测试类的名称应为:

Api::Users::AccountGroupsControllerTest