Testing Routes With RSpec and Subdomain Fu
11/25/2009 7:06:31 PMTesting subdomain fu based routes has really been causing me pain. Hope to wrap this up into a nice little patch package but maybe this will help others until time permits.
Example Route
1: map.foo_bar '/foo/bar', :controller => 'foo', :action => 'bar',
2: :conditions => { :subdomain => 'subdomain' }
Example Spec
1: it "should generate route for http://subdomain.test.com/foo/bar" do
2: controller.request.host = 'subdomain.test.com'
3: "/foo/bar".should route_to({ :controller => "foo", :action => 'bar' })
4: end
Overriding recognized_request_for in ControllerExampleGroup
The method recognized_request_for in ActionController::Assertions::RoutingAssertions uses a new controller for testing the routes. This fix leverages the controller for the spec instead.
1: module Spec
2: module Rails
3: module Example
4: class ControllerExampleGroup < FunctionalExampleGroup
5: def recognized_request_for(path, request_method = nil)
6: path = "/#{path}" unless path.first == '/'
7:
8: # Assume given controller
9: request = controller.request #ActionController::TestRequest.new
10: request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
11: request.path = path
12:
13: ActionController::Routing::Routes.recognize(request)
14: request
15: end
16: end
17: end
18: end
19: end