2014-10-17 36 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