ruby-on-rails - 在现有路由的 rspec 测试中找不到路由

标签 ruby-on-rails ruby rspec

rake 路线显示:

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

最佳答案

看起来您正在编写 Controller 规范(rails 称之为功能测试)

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

get :show, id: e.id

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

关于ruby-on-rails - 在现有路由的 rspec 测试中找不到路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28551578/

相关文章:

ruby-on-rails - PG::Rails 应用程序的语法错误

css - haml代码如何使其有一些缩进

ruby-on-rails - Rails,从 Google Analytics 获取特定屏幕的数据

ruby-on-rails - 为什么我的 rspec 测试失败?

ruby-on-rails - Rails 动态 link_to 的

ruby-on-rails - Ruby on Rails JPEG 转 PDF

javascript - rails 问题中的 select_tag 和 javascript

ruby - 数组的 attr_accessor?

ruby - 是否可以在 Rspec 标签中包含条件逻辑

ruby - FactoryGirl的麻烦