I have the following routes:
config/routes.rb:
Rails.application.routes.draw do
scope module: :api do
namespace :v1 do
resources :users
end
end
end
controllers/api/v1/users_controller.rb:
module Api
module V1
class UsersController < ApplicationController
def index
...
end
end
end
end
The controller specs are under the folder spec/controllers/api/v1/
.
spec/controllers/api/v1/users_controller_spec.rb:
module Api
module V1
RSpec.describe UsersController, type: :controller do
let!(:users) { create_list(:user, 10) }
describe 'GET /v1/users' do
before { get :index }
# Already tried `get '/v1/users'`, got the same error
it 'should return 10 users' do
expect(JSON.parse(response).size).to eq(10)
end
end
end
end
end
Once I run the tests I receive the following error:
ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"api/v1/users"}
Note that it "infers" the api
part from somewhere, but my routes are like these:
v1/users
v1/users/:id
...
How can I make it work?
RSpec.describe Api::V1::UsersController
instead of putting it insideApi::V1
module. – Grzegorz