Konrad Janczyk Konrad Janczyk - 3 months ago 18
Ruby Question

Rspec: "No route matches" on scoped api route

I've got problem with RSpec and scoped route. Route looks like this

namespace :api do
scope module: :v1, constrains: ApiConstraints.new(version: 1, default: true) do
resources :users, except:[:new]
end
end


and spec (spec/controllers/api/v1/users_controller_spec.rb) looks like this:

RSpec.describe Api::V1::UsersController, type: :controller do
let(:current_user) { FactoryGirl.create(:user) }

before do
allow(controller).to receive(:authenticate_request!).and_return(true)
end

describe 'GET #show' do
it 'should show user details' do
get api_user_path(current_user.id.to_s), nil, { 'Accept' => 'application/app.com; version=1' }
expect(response).to have_http_status(:success)
end
end
end


I'm getting this error:

ActionController::UrlGenerationError:
No route matches {:action=>"/api/users/57c6a615fd32bd11444f792f", :controller=>"api/v1/users"}


I've tried "api_user_url", :show and others but I think that the problem lays in that scope. My routes looks like this:

api_users GET /api/users(.:format) api/v1/users#index {:constrains=>#<ApiConstraints:0x007fe6824dce50 @version=1, @default=true>}
POST /api/users(.:format) api/v1/users#create {:constrains=>#<ApiConstraints:0x007fe6824dce50 @version=1, @default=true>}
edit_api_user GET /api/users/:id/edit(.:format) api/v1/users#edit {:constrains=>#<ApiConstraints:0x007fe6824dce50 @version=1, @default=true>}
api_user GET /api/users/:id(.:format) api/v1/users#show {:constrains=>#<ApiConstraints:0x007fe6824dce50 @version=1, @default=true>}
PATCH /api/users/:id(.:format) api/v1/users#update {:constrains=>#<ApiConstraints:0x007fe6824dce50 @version=1, @default=true>}
PUT /api/users/:id(.:format) api/v1/users#update {:constrains=>#<ApiConstraints:0x007fe6824dce50 @version=1, @default=true>}
DELETE /api/users/:id(.:format) api/v1/users#destroy {:constrains=>#<ApiConstraints:0x007fe6824dce50 @version=1, @default=true>}

Answer

The problem was in a typo 'constrains' in routes file

cope module: :v1, constrains: ApiConstraints.new(version: 1, default: true) do

should be 'constraints'