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

2023-12-09

耙路线显示:

 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 称之为功能测试)

在这些测试中get, post等方法期望第一个参数是操作的名称,第二个参数是选项的哈希值 - 它们绕过路由(尽管它们确实检查操作是否可路由)。相反,你会这样做

get :show, id: e.id

另一方面,在集成测试(请求规范或功能规范)中,您将使用实际路径(并且取决于您将使用的设置)visit or get, post等等,但它们会有所不同get方法)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在现有路由的 rspec 测试中找不到路由 的相关文章

随机推荐