All routing examples fail for a Rails 3.2 Engine with RSpec 2.10
rails-engines, rspec-rails, rspec2, ruby-on-rails-3.2
Solution
Update (2013-10-31)
RSpec 2.14 now fully supports engine routes:
module MyEngine
describe ExampleController do
routes { MyEngine::Engine.routes }
end
end
Add it to all routing specs:
# spec/spec_helper.rb
config.include Module.new {
def self.included(base)
base.routes { Reportr::Engine.routes }
end
}, type: :routing
The best solution I've been able to find so far is explicitly setting the route set that will be used during the tests:
RSpec.configure do |config|
config.before(:each, type: :controller) { @routes = Licensing::Engine.routes }
config.before(:each, type: :routing) { @routes = Licensing::Engine.routes }
end
Then I had to rewrite my routing examples to omit the namespace:
it 'routes to #index' do
get('/licenses').should route_to('licensing/licenses#index')
end
This seems like a bit of a hack, but it does make sense. I'm not testing whether Rails is able to mount an engine at a particular path. I'm only testing that my engine has correctly set up its own routes. Still, I'd rather not have to overwrite an instance variable to make it happen.
Here are another couple of good threads on this topic:
- How to test routes in a Rails 3.1 mountable engine
- How do I write a Rails 3.1 engine controller test in rspec?
Problem
I've created a Rails Engine in the normal fashion, installed RSpec, and generated a scaffold for a model, but I can't get any routing specs to pass. Here's one example: ``` describe Licensing::LicensesController do it 'routes to #index' do get('/licensing/licenses').should route_to('licensing/licenses#index') end end ``` I'm running the examples in the dummy app like this: ``` $ cd spec/dummy $ rake spec /Users/brandan/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ../routing/licensing/licenses_routing_spec.rb F Failures: 1) Licensing::LicensesController routes to #index Failure/Error: get('/licensing/licenses').should route_to('licensing/licenses#index') No route matches "/licensing/licenses" # /Users/brandan/repos/licensing/spec/routing/licensing/licenses_routing_spec.rb:5:in `block (2 levels) in <top (required)>' Finished in 0.04345 seconds 1 example, 1 failure ``` The engine is mounted correctly in the dummy app: ``` # spec/dummy/config/routes.rb Rails.application.routes.draw do mount Licensing::Engine => "/licensing" end ``` And I can go into the dummy app and fire up the console and get that route just fine: ``` 1.9.3p194 :001 > app.get('/licensing/licenses') Licensing::License Load (0.3ms) SELECT "licensing_licenses".* FROM "licensing_licenses" 200 1.9.3p194 :002 > app.response.body "<!DOCTYPE html>..." ``` There's some discrepancy between the dummy app and RSpec, and I can't figure out what it is. I've found several articles that claim to solve this problem, but none of them have helped, and several of them are specific to Rails 3.1: - Ryan Bigg's article on generating and testing engines - Matthew Ratzloff's article on testing engine routes in Rails 3.1 - Stefan Wienert's article on mountable engines - A mailing list message about testing routing helpers in Rails 3.2 Has anybody solved this problem in Rails 3.2/RSpec 2.10?