rails 4.2.0 ActionController :: UrlGenerationError

时间:2015-04-24 09:55:32

标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails

我已经升级了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

Mine Routes文件:

match 'messages/contact-us' => 'messages#contact_us', via: [:get]

rake routes log:

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

1 个答案:

答案 0 :(得分:4)

这个错误是在Spree 3.0中由this commit引入的,它清除了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控制器或Spree路由命名空间中任何内容的测试,您只需从项目的spec/spec_helper.rb或{{删除这一行。 1}}:

spec/rails_helper.rb

这是怎么发生的

  • 以前,使用config.include Spree::TestingSupport::ControllerRequests, type: :controller use_route内的process_spree_action参数指定了Spree引擎路由。它们不会干扰您自己命名空间下的控制器测试。
  • Rails 4.2已弃用process_spree_xhr_action
  • 狂欢更改为较新的use_route,现在已应用于包含routes { Spree::Core::Engine.routes }的所有内容。
  • 这都是因为Rspec有admittedly poor Engine support,所以社区一直在尽力使这些工具协同工作。