在现有路由的 rspec 测试中找不到路由 [英] No route found in rspec test for an existing route

查看:21
本文介绍了在现有路由的 rspec 测试中找不到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

耙路线显示:

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

还有我的 rspec 测试:

And my rspec test:

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),但它说没有这样的路径??

This does not make sense to me. There is a route, I passed an object (which Rails smartly grabs the ID from), but it's saying there is no such path??

推荐答案

看起来您正在编写控制器规范(rails 调用功能测试)

It looks like you are writing a controller spec (which rails calls functional tests)

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

In these tests the get, post etc methods expect the first argument to be the name of the action and the second a hash of options - they bypass routing (although they do check that the action is routable). You would instead do

get :show, id: e.id

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

In integration tests (request specs or feature specs) on the other hand you would use an actual path (and depending on the setup you would use either visit or get, post etc but they'd be different get methods)

这篇关于在现有路由的 rspec 测试中找不到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆