2010-12-15 22:34:19 +00:00
|
|
|
module Shoulda # :nodoc:
|
|
|
|
module Matchers
|
|
|
|
module ActionController # :nodoc:
|
|
|
|
|
|
|
|
# Ensures that requesting +path+ using +method+ routes to +options+.
|
|
|
|
#
|
|
|
|
# If you don't specify a controller, it will use the controller from the
|
|
|
|
# example group.
|
|
|
|
#
|
|
|
|
# +to_param+ is called on the +options+ given.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2012-12-20 05:04:27 +00:00
|
|
|
# it { should route(:get, '/posts').
|
2010-12-15 22:34:19 +00:00
|
|
|
# to(:controller => :posts, :action => :index) }
|
2013-10-15 22:12:17 +00:00
|
|
|
# it { should route(:get, '/posts').to('posts#index') }
|
2012-12-20 05:04:27 +00:00
|
|
|
# it { should route(:get, '/posts/new').to(:action => :new) }
|
|
|
|
# it { should route(:post, '/posts').to(:action => :create) }
|
|
|
|
# it { should route(:get, '/posts/1').to(:action => :show, :id => 1) }
|
2013-10-15 22:12:17 +00:00
|
|
|
# it { should route(:get, '/posts/1').to('posts#show', :id => 1) }
|
2012-12-20 05:04:27 +00:00
|
|
|
# it { should route(:get, '/posts/1/edit').to(:action => :edit, :id => 1) }
|
|
|
|
# it { should route(:put, '/posts/1').to(:action => :update, :id => 1) }
|
|
|
|
# it { should route(:delete, '/posts/1').
|
2010-12-15 22:34:19 +00:00
|
|
|
# to(:action => :destroy, :id => 1) }
|
2012-12-20 05:04:27 +00:00
|
|
|
# it { should route(:get, '/users/1/posts/1').
|
2010-12-15 22:34:19 +00:00
|
|
|
# to(:action => :show, :id => 1, :user_id => 1) }
|
2013-10-15 22:12:17 +00:00
|
|
|
# it { should route(:get, '/users/1/posts/1').
|
|
|
|
# to('posts#show', :id => 1, :user_id => 1) }
|
2010-12-15 22:34:19 +00:00
|
|
|
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
|
|
|
|
|
2013-01-08 17:29:01 +00:00
|
|
|
attr_reader :failure_message_for_should, :failure_message_for_should_not
|
2012-03-30 14:44:46 +00:00
|
|
|
|
2013-10-15 22:12:17 +00:00
|
|
|
def to(*args)
|
|
|
|
@params = RouteParams.new(args).normalize
|
2010-12-15 22:34:19 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_context(context)
|
|
|
|
@context = context
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(controller)
|
2012-03-30 14:44:46 +00:00
|
|
|
guess_controller!(controller)
|
2010-12-15 22:34:19 +00:00
|
|
|
route_recognized?
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
"route #{@method.to_s.upcase} #{@path} to/from #{@params.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-03-30 14:44:46 +00:00
|
|
|
def guess_controller!(controller)
|
|
|
|
@params[:controller] ||= controller.controller_path
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def route_recognized?
|
|
|
|
begin
|
|
|
|
@context.send(:assert_routing,
|
|
|
|
{ :method => @method, :path => @path },
|
|
|
|
@params)
|
|
|
|
|
2013-01-08 17:29:01 +00:00
|
|
|
@failure_message_for_should_not = "Didn't expect to #{description}"
|
2010-12-15 22:34:19 +00:00
|
|
|
true
|
|
|
|
rescue ::ActionController::RoutingError => error
|
2013-01-08 17:29:01 +00:00
|
|
|
@failure_message_for_should = error.message
|
2010-12-15 22:34:19 +00:00
|
|
|
false
|
2011-02-03 17:13:11 +00:00
|
|
|
rescue Shoulda::Matchers::AssertionError => error
|
2013-01-08 17:29:01 +00:00
|
|
|
@failure_message_for_should = error.message
|
2010-12-15 22:34:19 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|