Permalink
Browse files

Allow use of URL helpers even when using anonymous controller

- Closes #355.
- CLoses #339.
  • Loading branch information...
1 parent e077140 commit cdf69d0a7b76a089230929ed8c10e25ffa6a378f @alindeman alindeman committed with dchelimsky Apr 16, 2011
View
10 lib/rspec/rails/example/controller_example_group.rb
@@ -149,12 +149,22 @@ def name
after do
@routes = @orig_routes
+ @orig_routes = nil
end
end
end
module InstanceMethods
attr_reader :controller, :routes
+
+ def method_missing(method, *args, &block)
+ if @orig_routes && @orig_routes.named_routes.helpers.include?(method)
+ # Delegate to underlying controller
+ controller.send(method, *args, &block)
+ else
+ super
+ end
+ end
end
included do
View
25 spec/rspec/rails/example/controller_example_group_spec.rb
@@ -35,5 +35,30 @@ module RSpec::Rails
example.subject.should == 'explicit'
end
end
+
+ describe "with anonymous controller" do
+ before do
+ group.class_eval do
+ controller(Class.new) { }
+ end
+ end
+
+ it "delegates named route helpers to the underlying controller" do
+ controller = double('controller')
+ controller.should_receive(:foos_url).and_return('http://test.host/foos')
+
+ example = group.new
+ example.stub(:controller => controller)
+
+ # As in the routing example spec, this is pretty invasive, but not sure
+ # how to do it any other way as the correct operation relies on before
+ # hooks
+ orig_routes = ActionDispatch::Routing::RouteSet.new
+ orig_routes.draw { resources :foos }
+ example.instance_variable_set(:@orig_routes, orig_routes)
+
+ example.foos_url.should be
+ end
+ end
end
end

0 comments on commit cdf69d0

Please sign in to comment.