我已经升级了 rails 4.2.0 和 spree 3.0

一旦我运行 rspec 然后我得到以下错误,它在 rails 4.2.0 中向我显示 rspec ActionController::UrlGenerationError,我有很多 googled 但没有找到任何解决方案,请在下面的日志中找到:

Run options: include {:locations=>{"./spec/controllers/messages_controller_spec.rb"=>[10]}}
F

Failures:

  1) MessagesController create with valid message sends message and shows flash notice
     Failure/Error: get "contact-us"
     ActionController::UrlGenerationError:
       No route matches {:action=>"contact-us", :controller=>"messages"}
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/journey/formatter.rb:46:in `generate'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:727:in `generate'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:758:in `generate'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:753:in `generate_extras'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:748:in `extra_keys'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:208:in `assign_parameters'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:619:in `process'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:65:in `process'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:19:in `block in process'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:72:in `catch'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:72:in `_catch_warden'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:19:in `process'
     # /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:508:in `get'
     # ./spec/controllers/messages_controller_spec.rb:11:in `block (4 levels) in <top (required)>'

Finished in 0.02445 seconds (files took 9.1 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/messages_controller_spec.rb:10 # MessagesController create with valid message sends message and shows flash notice

我的路线文件:
match 'messages/contact-us' => 'messages#contact_us', via: [:get]

rake 路线日志:
rake routes | grep "contact-us"
                                         contact_us GET    /contact-us(.:format)                                          redirect(301, /support#/contact-us)
                                            contact GET    /contact(.:format)                                             redirect(301, /support#/contact-us)
                                messages_contact_us GET    /messages/contact-us(.:format)                                 messages#contact_us

最佳答案

这个错误是由 this commit 在 Spree 3.0 中引入的,它清理了 Spree Controller 测试指定使用哪些路由的方式。您可能会看到此错误,因为您正在测试 Spree::Core::Engine.routes 之外的路由,但您的 spec_helper.rb 仍包含以下行:

config.include Spree::TestingSupport::ControllerRequests, type: :controller

怎么修

选项1)

通过重新分配用于测试的路由来修复有问题的规范。例子:
describe MessagesController, type: :controller do
  routes { Rails.application.routes }  # <--- add this line

  describe '#index' do
    before do
      get :index
    end

    specify do
      expect(response).to be_success
    end
  end
end

选项 2)

如果您的项目中没有任何命中 Spree Controller 或 Spree 路由命名空间中的任何测试,您可以简单地 从项目的 spec/spec_helper.rbspec/rails_helper.rb 中删除 这一行:
config.include Spree::TestingSupport::ControllerRequests, type: :controller

这是怎么发生的
  • 以前,Spree 引擎路由是使用 use_routeprocess_spree_action 中的 process_spree_xhr_action 参数指定的。它们不会干扰您自己的命名空间下的 Controller 测试。
  • Rails 4.2 已弃用 use_route
  • Spree 更改为较新的 routes { Spree::Core::Engine.routes } ,现在适用于包含 Spree::TestingSupport::ControllerRequests 的所有内容。
  • 这都是因为 Rspec 有 admittedly poor Engine support ,所以社区一直在尽最大努力让这些工具协同工作。
  • 关于ruby-on-rails - rails 4.2.0 ActionController::UrlGenerationError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29844403/

    10-13 02:08