具有嵌套路由的api失败的规范

时间:2014-10-17 05:54:27

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

我正在努力将一个api添加到rails todo list项目中的ruby作为学习练习,并且我在测试一些使用嵌套路由的规范时遇到了问题。

我希望在使用/ lists / 1 / endpoint时显示未完成的任务列表,但是当我运行我的规范时,会有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

1 个答案:

答案 0 :(得分:2)

看起来问题是你发送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