【问题标题】:Specs failing for api with nested routes具有嵌套路由的 api 规范失败
【发布时间】:2014-12-12 15:54:10
【问题描述】:

我正在为 ruby​​ on rails 待办事项列表项目添加一个 api 作为学习练习,但在测试一些使用嵌套路由的规范时遇到问题。

我想在使用 /lists/1/ 端点时显示未完成任务的列表,但是当我运行我的规范时,出现 UrlGenerationErrors 并且没有路由匹配。

关于如何解决这个问题有什么想法吗?是routes.rb的错误吗?

仅供参考,我确实有响应 /user/1/lists 和 /lists/ 的索引,因此我还可以显示用户拥有哪些列表以及整个应用程序中存在哪些公共列表。这就是让应用感到困惑的原因吗?

谢谢!

规格

  describe "#show" do
    ...

    context "authorized user is the owner of the list" do
      it "returns all uncompleted items" do
        params = {list_id: @personal_list.id}
        get :show, params

        expect(response.status).to eq(200) 
        #expect(json).to json
      end
    end

    context "authorized user when looking at a nonprivate list" do
      it "returns all uncompleted items" do
        params = {list_id: @open_list.id}
        get :show, params

        expect(response.status).to eq(200)
        #expect(json).to json
      end
    end   

    context "authorized user when looking at a private list" do
      it "returns error" do
        authWithToken(@api.access_token)
        params = {list_id: @private_list.id}
        get :show, params

        expect(response.status).to eq(400) 
      end
    end
  end 

Failure Messages

:V1::ListsController#show authorized user is the owner of the list returns all uncompleted items
     Failure/Error: get :show, params
     ActionController::UrlGenerationError:
       No route matches {:list_id=>"1", :controller=>"api/v1/lists", :action=>"show"}
     # ./spec/controllers/api/v1/lists_controller_spec.rb:109:in `block (4 levels) in <top (required)>'

  2) Api::V1::ListsController#show authorized user when looking at a nonprivate list returns all uncompleted items
     Failure/Error: get :show, params
     ActionController::UrlGenerationError:
       No route matches {:list_id=>"2", :controller=>"api/v1/lists", :action=>"show"}
     # ./spec/controllers/api/v1/lists_controller_spec.rb:126:in `block (4 levels) in <top (required)>'

  3) Api::V1::ListsController#show authorized user when looking at a private list returns error
     Failure/Error: get :show, params
     ActionController::UrlGenerationError:
       No route matches {:list_id=>"3", :controller=>"api/v1/lists", :action=>"show"}
     # ./spec/controllers/api/v1/lists_controller_spec.rb:143:in `block (4 levels) in <top (required)>'

路线

namespace :api, defaults: {format: 'json'} do
    namespace :v1 do
      resources :users, except: [:destroy, :new] do 
        resources :lists, only: [:index]
      end

      resources :lists, only: [:index, :show, :create, :update, :destroy] do
        resources :items, only: [:create, :update, :destroy]
      end

      resources :items, only: [:destroy]
    end
  end

       Prefix Verb   URI Pattern                                Controller#Action
api_v1_user_lists GET    /api/v1/users/:user_id/lists(.:format)     api/v1/lists#index {:format=>"json"}
     api_v1_users GET    /api/v1/users(.:format)                    api/v1/users#index {:format=>"json"}
                  POST   /api/v1/users(.:format)                    api/v1/users#create {:format=>"json"}
 edit_api_v1_user GET    /api/v1/users/:id/edit(.:format)           api/v1/users#edit {:format=>"json"}
      api_v1_user GET    /api/v1/users/:id(.:format)                api/v1/users#show {:format=>"json"}
                  PATCH  /api/v1/users/:id(.:format)                api/v1/users#update {:format=>"json"}
                  PUT    /api/v1/users/:id(.:format)                api/v1/users#update {:format=>"json"}
api_v1_list_items POST   /api/v1/lists/:list_id/items(.:format)     api/v1/items#create {:format=>"json"}
 api_v1_list_item PATCH  /api/v1/lists/:list_id/items/:id(.:format) api/v1/items#update {:format=>"json"}
                  PUT    /api/v1/lists/:list_id/items/:id(.:format) api/v1/items#update {:format=>"json"}
                  DELETE /api/v1/lists/:list_id/items/:id(.:format) api/v1/items#destroy {:format=>"json"}
     api_v1_lists GET    /api/v1/lists(.:format)                    api/v1/lists#index {:format=>"json"}
                  POST   /api/v1/lists(.:format)                    api/v1/lists#create {:format=>"json"}
      api_v1_list GET    /api/v1/lists/:id(.:format)                api/v1/lists#show {:format=>"json"}
                  PATCH  /api/v1/lists/:id(.:format)                api/v1/lists#update {:format=>"json"}
                  PUT    /api/v1/lists/:id(.:format)                api/v1/lists#update {:format=>"json"}
                  DELETE /api/v1/lists/:id(.:format)                api/v1/lists#destroy {:format=>"json"}
      api_v1_item DELETE /api/v1/items/:id(.:format)                api/v1/items#destroy {:format=>"json"}

API 的列表控制器

  def show
    @list = List.find(params[:id])
    if (@list.permissions == "open") || (@list.user_id == @authorized_user)
      render json: @list.items.completed, each_serializer: ItemSerializer
    else
      render json: @list.errors, status: :error
    end
  end

【问题讨论】:

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


    【解决方案1】:

    看起来问题在于您发送的是 list_id 的参数而不是 id。

    #show 的默认 rails 路由会查找 :id,这也是您在控制器中(正确)使用的。

    尝试如下重写您的测试:

    describe "#show" do
    ...
      context "authorized user is the owner of the list" do
        it "returns all uncompleted items" do
          get :show, :id => @personal_list.id
      end
    
    OR
    
      context "authorized user is the owner of the list" do
        it "returns all uncompleted items" do
          params = {id: @private_list.id}
          get :show, params
      end
    ...
    end 
    

    要确保的另一件事是您使用了正确的顶级描述。因为这是一个控制器规范,所以需要匹配控制器名称。即:

    describe Api::V1::ListsController do
      describe 'show' do
    

    【讨论】:

    • 是的,成功了,在我意识到过滤器之前没有运行为 show 方法设置列表的方法之后,规范通过了
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多