【问题标题】:Rspec can't find route in controller testRspec 在控制器测试中找不到路由
【发布时间】:2015-09-16 18:55:57
【问题描述】:

在使用 Rspec 3 测试 Rails 3.0 应用程序时,我不断收到路由错误。它无法检测到我用于获取请求的路由。这是我的文件的样子:

routes.rb

resources :proficiency_tests do
  member do
    resource :lag_components, only: [:edit, :update]
  end
end

运行 rake:routes 返回:

edit_lag_components GET    /proficiency_tests/:id/lag_components/edit(.:format)    {:action=>"edit", :controller=>"lag_components"}
     lag_components PUT    /proficiency_tests/:id/lag_components(.:format)         {:action=>"update", :controller=>"lag_components"}

控制器在:

app
|---controllers
    |---lag_components_controller.rb

控制器的设置方式没有什么特别之处。

规格在:

spec
    |---controllers
        |---lag_components_controller.rb

测试设置如下:

需要'spec_helper'

describe LagSubmissionsController do
  describe 'GET #edit' do
    let(:proficiency_test) { create :proficiency_test }

    it 'redirects' do
      *tests*    
    end
  end
end

这是我尝试过的以及错误消息是什么:

get(:edit, id: proficiency_test.id)
# => No route matches {:id=>11259, :controller=>"lag_submissions", :action=>"edit"}

get(:edit, id: proficiency_test.id, use_route: :edit_lag_components)
# => No route matches {:id=>11260, :controller=>"lag_submissions", :action=>"edit"}

get("/proficiency_tests/#{proficiency_test.id}/lag_components/edit")
# => No route matches {:controller=>"lag_submissions", :action=>"/proficiency_tests/11258/lag_components/edit"}

最后一个特别有趣,因为那是我在浏览器中用来渲染edit页面的确切路径:

有谁知道发生了什么以及为什么找不到那条路线?不幸的是,这是一个较旧的应用程序,我无法更改周围的路线。

【问题讨论】:

  • 你为错误的控制器编写了 rspec

标签: ruby-on-rails ruby-on-rails-3 rspec rspec3


【解决方案1】:

路由定义为 lag_components,但您的控制器称为 LagSubmissions。资源名称需要与控制器名称匹配 - 因此将控制器名称更改为 LagComponentsController。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多