2014-10-17 3 views
2

Ruby에서 레일 위드 목록 프로젝트에 api를 추가하는 중입니다. 중첩 된 경로를 사용하는 일부 사양을 테스트하는 데 문제가 있습니다.중첩 된 경로가있는 API에 대한 사양이 잘못되었습니다.

/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 

답변

2

그것은 홍보처럼 보인다 oblem은 id 대신 list_id의 param을 전송한다는 것입니다.

#show의 기본 레일 라우팅은 컨트롤러에서 (올바르게) 사용했던 : 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 

보장하기 위해 다른 것은 올바른 최고 수준의 설명을 사용하는 것입니다. 이것은 컨트롤러 사양이기 때문에 컨트롤러 이름과 일치해야합니다. 즉 :

+1

예, 필터가 show 메서드의 목록을 설정하는 메서드를 실행하지 않는 것을 알게 된 후 사양이 통과되었습니다. –

관련 문제