This question has probably been asked a dozen times on Stack Overflow (e.g. (1), (2), (3), (4), (5)) but every time the answer seems to be different and none of them have helped me. I’m working on a Rails Engine and I’m finding that Rspec2 gets route errors, but I can reach the routes in the browser. Here’s the situation:
-
In the engine’s
routes.rb:resources :mw_interactives, :controller => 'mw_interactives', :constraints => { :id => /\d+/ }, :except => :show # This is so we can build the InteractiveItem at the same time as the Interactive resources :pages, :controller => 'interactive_pages', :constraints => { :id => /\d+/ }, :only => [:show] do resources :mw_interactives, :controller => 'mw_interactives', :constraints => { :id => /\d+/ }, :except => :show end -
Excerpted output of
rake routes:new_mw_interactive GET /mw_interactives/new(.:format) lightweight/mw_interactives#new {:id=>/\d+/} ... new_page_mw_interactive GET /pages/:page_id/mw_interactives/new(.:format) lightweight/mw_interactives#new {:id=>/\d+/, :page_id=>/\d+/} -
And my test, from one of the controller specs (
describe Lightweight::MwInteractivesController do):it 'shows a form for a new interactive' do get :new end
…which gets this result:
Failure/Error: get :new
ActionController::RoutingError:
No route matches {:controller=>"lightweight/mw_interactives", :action=>"new"}
…and yet when I go to that route in the browser, it works exactly as intended.
What am I missing here?
ETA: To clarify a point Andreas raises: this is a Rails Engine, so rspec runs in a dummy application which includes the engine’s routes in a namespace:
mount Lightweight::Engine => "/lightweight"
…so the routes shown in rake routes are prefaced with /lightweight/. That’s why the route shown in the Rspec error doesn’t seem to match what’s in rake routes. But it does make the debugging an extra step wonkier.
ETA2: Answering Ryan Clark’s comment, this is the action I’m testing:
module Lightweight
class MwInteractivesController < ApplicationController
def new
create
end
…and that’s it.
I found a workaround for this. Right at the top of the spec, I added this code:
…and now the spec runs without the routing error. But I don’t know why this works, so if someone can post an answer which explains it, I’ll accept that.