Ruby on rails Rspec:“是的;“没有路线匹配”;关于作用域api路由
我对RSpec和作用域路由有问题。路线是这样的Ruby on rails Rspec:“是的;“没有路线匹配”;关于作用域api路由,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我对RSpec和作用域路由有问题。路线是这样的 namespace :api do scope module: :v1, constrains: ApiConstraints.new(version: 1, default: true) do resources :users, except:[:new] end end 规范(spec/controllers/api/v1/users\u controller\u spec.rb)如下所示: RSpe
namespace :api do
scope module: :v1, constrains: ApiConstraints.new(version: 1, default: true) do
resources :users, except:[:new]
end
end
规范(spec/controllers/api/v1/users\u controller\u spec.rb)如下所示:
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
我得到了这个错误:
ActionController::UrlGenerationError:
No route matches {:action=>"/api/users/57c6a615fd32bd11444f792f", :controller=>"api/v1/users"}
我尝试过“api\u user\u url”、:show和其他方法,但我认为问题在于这个范围。我的路线如下所示:
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>}
api_users GET/api/users(:format)api/v1/users#index{:constraints=>
POST/api/users(:format)api/v1/users 35; create{:constraints=>#
edit_api_user GET/api/users/:id/edit(:format)api/v1/users#edit{:constraints=>#
api_user GET/api/users/:id(:format)api/v1/users 35; show{:constraints=>#
PATCH/api/users/:id(:format)api/v1/users#update{:constraints=>#}
PUT/api/users/:id(:format)api/v1/users#update{:constraints=>#
DELETE/api/users/:id(:format)api/v1/users#destroy{:constraints=>#}
问题在于routes文件中的输入错误“Constraints”:
scope module: :v1, constrains: ApiConstraints.new(version: 1, default: true) do
应该是“约束”,只缺少
t
。那么get-api\u-user\u-path(当前用户)
呢?不,它在“和其他”部分。事实上,这是我的第一个方法。抱歉说得这么含糊!