Rspec:在路由的api路由

时间:2016-08-31 09:47:03

标签: 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(spec / controllers / api / v1 / users_controller_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_user_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>}

1 个答案:

答案 0 :(得分:0)

问题出现在路线档

中的拼写错误'约束'
cope module: :v1, constrains: ApiConstraints.new(version: 1, default: true) do

应该是'约束'