I'm trying to test a Rails 3.1 engine with RSpec 2. After a lot of trial and error (and documentation and Stack Overflow searching) the app is working and I've gotten most of the specs to pass. The problem is that my route specs are still failing.
For an engine "foo" with an isolated namespace and a controller Foo::BarsController, I have this:
require "spec_helper"
describe Foo::BarsController do
describe "routing" do
it "routes to #index" do
get("/foo/bars").should route_to("bars#index")
end
it "routes to #new" do
get("/foo/bars/new").should route_to("bars#new")
end
end
end
This results in:
1) Foo::BarsController routing routes to #index
Failure/Error: get("/foo/bars").should route_to("bars#index")
ActionController::RoutingError:
No route matches "/foo/bars"
# ./spec/routing/foo/bars_routing_spec.rb:6:in `block (3 levels) in <top (required)>'
2) Foo::BarsController routing routes to #new
Failure/Error: get("/foo/bars/new").should route_to("bars#new")
ActionController::RoutingError:
No route matches "/foo/bars/new"
# ./spec/routing/foo/bars_routing_spec.rb:10:in `block (3 levels) in <top (required)>'
My spec dummy application seems to be set up correctly:
Rails.application.routes.draw do
mount Foo::Engine => "/foo"
end
If it helps to answer this question, my view specs are also not working. Here is a typical error:
9) foo/bars/index.html.erb renders a list of bars
Failure/Error: render
ActionView::Template::Error:
undefined local variable or method `new_bar_path' for #&l开发者_StackOverflowt;#<Class:0x00000100c14958>:0x0000010464ceb8>
# ./app/views/foo/bars/index.html.erb:3:in `___sers_matt__ites_foo_app_views_foo_bars_index_html_erb___1743631507081160226_2184232780'
# ./spec/views/foo/bars/index.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'
Any ideas?
Try changing your get
method to use the specific route
get("/foo/bars", :use_route => :foo)
OK, I got this working! And wrote a blog post about it.
http://www.matthewratzloff.com/blog/2011/09/21/testing-routes-with-rails-3-1-engines/
It requires a new method to import engine routes into the application route set for tests.
Edit: Caught a bug with named route handling and fixed it.
It's actually not so hard to get this to work. It's a bit hacky since you are adding code to the engine's routes.rb file that changes depending on which env it's running in. If you are using the spec/dummy site approach for testing the engine then use the following code snippet in /config/routes.rb file:
# <your_engine>/config/routes.rb
if Rails.env.test? && Rails.application.class.name.to_s == 'Dummy::Application'
application = Rails.application
else
application = YourEngine::Engine
end
application.routes.draw do
...
end
What this is basically doing is switching out your engine for the dummy app and writing the routes to it when in test mode.
Here is the official rspec documentation solution:
https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs/engine-routes
精彩评论