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
是這樣做的技巧,規範之後,我意識到,過濾器沒有運行之前設置列表的方法show方法 – 2014-10-17 14:33:32