2014-10-17 37 views
2

我正在努力添加一个api红宝石轨道待办事项列表项目作为学习练习,我有问题测试使用嵌套路线的一些规格。规格失败与嵌套路线的API

我想使用/ lists/1/endpoint时显示未完成任务的列表,但是当我运行我的规格时,有UrlGenerationErrors和没有路由匹配。

有关如何解决这个问题的任何想法?在routes.rb中是否有错误?

仅供参考,我确实有指标响应/用户/ 1 /列表和/列表/所以我也可以显示什么列表是由用户和哪些公共清单,在整个应用程序存在拥有。这是什么让应用程序混淆?

谢谢!

规格

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 

回答

2

它看起来像公关问题是你正在发送list_id的参数而不是id。

默认轨的路由#show寻找一个:身份证这也是你使用什么(正确)的控制器。

尝试重新写你的测试如下:

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 
+1

是这样做的技巧,规范之后,我意识到,过滤器没有运行之前设置列表的方法show方法 – 2014-10-17 14:33:32