Permalink
Browse files

Converted should_route to use a matcher

  • Loading branch information...
1 parent 8823496 commit 10637cf96477b7c137eba639e3a365c9ec26dcd0 @jferris jferris committed Feb 5, 2009
View
@@ -1,7 +1,6 @@
Unfinished:
should_respond_with
should_respond_with_content_type
-should_route
Finished:
should_assign_to
@@ -319,19 +319,11 @@ def should_route(method, path, options)
unless options[:controller]
options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
end
- options[:controller] = options[:controller].to_s
- options[:action] = options[:action].to_s
- populated_path = path.dup
- options.each do |key, value|
- options[key] = value.to_param if value.respond_to? :to_param
- populated_path.gsub!(key.inspect, value.to_s)
- end
+ matcher = route(method, path).to(options)
- should_name = "route #{method.to_s.upcase} #{populated_path} to/from #{options.inspect}"
-
- should should_name do
- assert_routing({:method => method, :path => populated_path}, options)
+ should matcher.description do
+ assert_accepts matcher.in_context(self), self
end
end
end
@@ -4,6 +4,7 @@
require 'shoulda/controller/matchers/render_with_layout_matcher'
require 'shoulda/controller/matchers/respond_with_matcher'
require 'shoulda/controller/matchers/set_session_matcher'
+require 'shoulda/controller/matchers/route_matcher'
module Shoulda # :nodoc:
module Controller # :nodoc:
@@ -0,0 +1,73 @@
+module Shoulda # :nodoc:
+ module Controller # :nodoc:
+ module Matchers
+
+ def route(method, path)
+ RouteMatcher.new(method, path, self)
+ end
+
+ class RouteMatcher # :nodoc:
+
+ def initialize(method, path, context)
+ @method = method
+ @path = path
+ @context = context
+ end
+
+ def to(params)
+ @params = params
+ self
+ end
+
+ def in_context(context)
+ @context = context
+ self
+ end
+
+ def matches?(controller)
+ @controller = controller
+ guess_controller!
+ stringify_params!
+ route_recognized?
+ end
+
+ attr_reader :failure_message, :negative_failure_message
+
+ def description
+ "route #{@method.to_s.upcase} #{@path} to/from #{@params.inspect}"
+ end
+
+ private
+
+ def guess_controller!
+ @params[:controller] ||= @controller.controller_path
+ end
+
+ def stringify_params!
+ @params.each do |key, value|
+ @params[key] = value.to_param
+ end
+ end
+
+ def route_recognized?
+ begin
+ @context.send(:assert_routing,
+ { :method => @method, :path => @path },
+ @params)
+
+ @negative_failure_message = "Didn't expect to #{description}"
+ true
+ rescue ActionController::RoutingError => error
+ @failure_message = error.message
+ false
+ rescue Test::Unit::AssertionFailedError => error
+ @failure_message = error.message
+ false
+ end
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,58 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
+
+class RouteToMatcherTest < Test::Unit::TestCase # :nodoc:
+
+ context "given a controller with a defined route" do
+ setup do
+ @controller = define_controller('Examples').new
+ define_routes do |map|
+ map.connect 'examples/:id', :controller => 'examples',
+ :action => 'example'
+ end
+ end
+
+ should "accept routing the correct path to the correct parameters" do
+ assert_accepts route(:get, '/examples/1').
+ to(:action => 'example', :id => '1'),
+ @controller
+ end
+
+ should "accept a symbol controller" do
+ assert_accepts route(:get, '/examples/1').
+ to(:controller => :examples,
+ :action => 'example',
+ :id => '1'),
+ self
+ end
+
+ should "accept a symbol action" do
+ assert_accepts route(:get, '/examples/1').
+ to(:action => :example, :id => '1'),
+ @controller
+ end
+
+ should "accept a non-string parameter" do
+ assert_accepts route(:get, '/examples/1').
+ to(:action => 'example', :id => 1),
+ @controller
+ end
+
+ should "reject an undefined route" do
+ assert_rejects route(:get, '/bad_route').to(:var => 'value'), @controller
+ end
+
+ should "reject a route for another controller" do
+ @other = define_controller('Other').new
+ assert_rejects route(:get, '/examples/1').
+ to(:action => 'example', :id => '1'),
+ @other
+ end
+
+ should "reject a route for different parameters" do
+ assert_rejects route(:get, '/examples/1').
+ to(:action => 'other', :id => '1'),
+ @controller
+ end
+ end
+
+end

0 comments on commit 10637cf

Please sign in to comment.