2

I'm working on adding an api to a ruby on rails todo list project as a learning exercise and I'm having problems testing some specs that use nested routes.

I'd like to show a list of uncompleted tasks when the /lists/1/ endpoint is used, but when I run my specs, there are UrlGenerationErrors and no route matches.

Any thoughts about how I can fix this? Is it a mistake in routes.rb?

FYI, I do have index responding to /user/1/lists and /lists/ so I can also show what lists are owned by a user and what public lists exist across the app. Is this what's confusing the app?

Thanks!

Specs

  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)>'

Routes

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"}

Lists Controller for 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
Tien Yuan
  • 323
  • 1
  • 2
  • 9

1 Answers1

2

It looks like the problem is that you're sending the param of list_id instead of id.

Default rails routing for #show looks for an :id which is also what you've used (correctly) in your controller.

Try re-writing your tests as follows:

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 

The other thing to ensure is that you are using the correct top level description. Because this is a controller spec this needs to match the controller name. ie:

describe Api::V1::ListsController do
  describe 'show' do
Allan W Smith
  • 756
  • 6
  • 20
  • 1
    yes that did the trick, the spec passed after I realized that before filter wasn't running the method that set the list for the show method – Tien Yuan Oct 17 '14 at 14:33