使用RSpec访问嵌套路由中的控制器操作时遇到问题

时间:2016-03-24 15:36:01

标签: ruby-on-rails ruby rspec nested-routes

我正在使用以下路线:

resources :groups, :only => [:show, :create, :update, :destroy] do
    get 'viewmembers' => 'groups#view_members"
end

但是当我试图在RSpec中测试时:

describe "GET #viewmembers" do

context "the group members are retrieved" do
    before do
      @group = FactoryGirl.create :group
      ..
      get :viewmembers, group_id: @group.id
    end

    it { should respond_with :ok}
    # others tests...
end

我收到以下错误:

 Failure/Error: get 'viewmembers', group_id: @group.id

 ActionController::UrlGenerationError:
   No route matches {:action=>"viewmembers", :controller=>"api/v1/groups", :group_id=>"1"}

我在Rspec测试中应该采用不同的方式来制定GET请求吗?

以下是rake routes中列出的内容:

GET    /api/v1/groups/:group_id/viewmembers(.:format)    api/v1/group#viewmembers{:format=>:json}`

如果我采用嵌套的do-end块的get 'viewmembers'...路由OUT作为组资源,如下所示:

resources :groups, :only => [:show, :create, :update, :destroy]
get '/groups/viewmembers' => 'groups#view_members'

然后rspec测试运行正常,因此我必须解决我制定请求的方式...

0 个答案:

没有答案