在现有路线的rspec测试中找不到路线

耙路线显示:

estimate_location GET /estimate/location/:id(.:format) estimate/location#show 

我的rspec测试:

 it 'should re-direct to location/new from show' do e = FactoryGirl.create :estimate get estimate_location_path e expect(response.status).to eq(302) end 

控制台说:

  Failure/Error: get estimate_location_path e ActionController::RoutingError: No route matches {:controller=>"estimate/location", :action=>"/estimate/location/1"} 

这对我来说没有意义。 有一条路线,我传递了一个对象(Rails巧妙地抓住了ID),但是它说没有这样的路径?

看起来你正在编写一个控制器规范(rails调用function测试)

在这些测试中, getpost等方法期望第一个参数是操作的名称,第二个参数是选项的散列 – 它们绕过路由(尽管它们检查操作是否可路由)。 你会这样做

 get :show, id: e.id 

另一方面,在集成测试(请求规范或function规范)中,您将使用实际路径(并且根据您将使用的设置visitgetpost等,但它们是不同的get方法)

尝试重写您的测试

 it 'should re-direct to location/new from show' do let(:estimate) {FactoryGirl.create(:estimate)} before {get estimate_location_path(estimate)} expect(response.status).to eq(302) end 

我遇到过同样的问题。 使用引擎的应用程序没有映射路由的问题,但rspec无法找到路由。

几个小时后,我重写了我的路线文件:

Authentication::Engine.routes.draw do namespace :api do namespace :v1 do post '/auth_token/:id', to: Authentication::Api::V1::AuthTokenController.action(:create), as: :auth_token end end end

Authentication::Engine.routes.draw do namespace :api do namespace :v1 do post '/auth_token/:id' => 'auth_token#create', as: :auth_token end end end

似乎rspec不够聪明,无法对第一个片段中的路由写回控制器进行逆向工程。