user3118220
user3118220

Reputation: 1468

rails 4.2.0 ActionController::UrlGenerationError

I have upgraded rails 4.2.0 and spree 3.0

Once I run rspec then I am getting following error, It is showing me rspec ActionController::UrlGenerationError in rails 4.2.0, I have lots of googled but did not find any solution, Please find below log:

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 file:

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

Upvotes: 5

Views: 760

Answers (1)

Marc L
Marc L

Reputation: 2092

This bug was introduced in Spree 3.0, by this commit which cleans up the way Spree Controller tests specify which routes to use. You are likely seeing this error because you are testing a route that is outside of Spree::Core::Engine.routes, but your spec_helper.rb still includes this line:

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

How to fix

Option 1)

Fix just the spec in question by reassigning the routes used for the test. Example:

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

Option 2)

If you do NOT have any tests in your project that hit Spree controllers, or anything in the Spree route namespace, you can simply remove this line from your project's spec/spec_helper.rb or spec/rails_helper.rb:

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

How this happened

  • Previously, the Spree engine routes were specified with the use_route parameter inside of process_spree_action and process_spree_xhr_action. They did not interfere with controller tests under your own namespace.
  • Rails 4.2 deprecated use_route.
  • Spree changed to the newer routes { Spree::Core::Engine.routes }, which is now applied to everything that includes Spree::TestingSupport::ControllerRequests.
  • This is all because Rspec has admittedly poor Engine support, so the community has been doing their best to make the tools work together.

Upvotes: 5

Related Questions