2010-12-15 22:34:19 +00:00
|
|
|
module Shoulda # :nodoc:
|
|
|
|
module Matchers
|
|
|
|
module ActionController # :nodoc:
|
|
|
|
# Ensures a controller redirected to the given url.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# it { should redirect_to('http://somewhere.com') }
|
|
|
|
# it { should redirect_to(users_path) }
|
|
|
|
def redirect_to(url_or_description, &block)
|
|
|
|
RedirectToMatcher.new(url_or_description, self, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
class RedirectToMatcher # :nodoc:
|
2013-12-24 11:24:27 +00:00
|
|
|
attr_reader :failure_message, :failure_message_when_negated
|
|
|
|
|
|
|
|
alias failure_message_for_should failure_message
|
|
|
|
alias failure_message_for_should_not failure_message_when_negated
|
2010-12-15 22:34:19 +00:00
|
|
|
|
|
|
|
def initialize(url_or_description, context, &block)
|
|
|
|
if block
|
|
|
|
@url_block = block
|
2010-12-17 16:50:49 +00:00
|
|
|
@location = url_or_description
|
2010-12-15 22:34:19 +00:00
|
|
|
else
|
|
|
|
@url = url_or_description
|
|
|
|
@location = @url
|
|
|
|
end
|
|
|
|
@context = context
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_context(context)
|
|
|
|
@context = context
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(controller)
|
|
|
|
@controller = controller
|
|
|
|
redirects_to_url?
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
"redirect to #{@location}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def redirects_to_url?
|
|
|
|
begin
|
2014-02-01 21:09:44 +00:00
|
|
|
@context.__send__(:assert_redirected_to, url)
|
2013-12-24 11:24:27 +00:00
|
|
|
@failure_message_when_negated = "Didn't expect to redirect to #{url}"
|
2010-12-15 22:34:19 +00:00
|
|
|
true
|
2011-02-03 17:13:11 +00:00
|
|
|
rescue Shoulda::Matchers::AssertionError => error
|
2013-12-24 11:24:27 +00:00
|
|
|
@failure_message = error.message
|
2010-12-15 22:34:19 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-30 15:22:31 +00:00
|
|
|
def url
|
|
|
|
if @url_block
|
|
|
|
@context.instance_eval(&@url_block)
|
|
|
|
else
|
|
|
|
@url
|
|
|
|
end
|
|
|
|
end
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|