Skip to content

Commit

Permalink
Added a redirect_to matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jun 7, 2010
1 parent be6e6e7 commit 0fe568f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/shoulda/action_controller/matchers.rb
Expand Up @@ -6,6 +6,7 @@
require 'shoulda/action_controller/matchers/respond_with_content_type_matcher'
require 'shoulda/action_controller/matchers/set_session_matcher'
require 'shoulda/action_controller/matchers/route_matcher'
require 'shoulda/action_controller/matchers/redirect_to_matcher'

module Shoulda # :nodoc:
module ActionController # :nodoc:
Expand Down
62 changes: 62 additions & 0 deletions lib/shoulda/action_controller/matchers/redirect_to_matcher.rb
@@ -0,0 +1,62 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
module Matchers

# 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:

def initialize(url_or_description, context, &block)
if block
@url_block = block
@location = @url_or_description
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

attr_reader :failure_message, :negative_failure_message

def description
"redirect to #{@location}"
end

private

def redirects_to_url?
@url = @context.instance_eval(&@url_block) if @url_block
begin
@context.send(:assert_redirected_to, @url)
@negative_failure_message = "Didn't expect to redirect to #{@url}"
true
rescue Test::Unit::AssertionFailedError => error
@failure_message = error.message
false
end
end

end

end
end
end
37 changes: 37 additions & 0 deletions test/matchers/controller/redirect_to_matcher_test.rb
@@ -0,0 +1,37 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')

class RedirectToMatcherTest < ActionController::TestCase # :nodoc:

context "a controller that redirects" do
setup do
@controller = build_response { redirect_to '/some/url' }
end

should "accept redirecting to that url" do
assert_accepts redirect_to('/some/url'), @controller
end

should "reject redirecting to a different url" do
assert_rejects redirect_to('/some/other/url'), @controller
end

should "accept redirecting to that url in a block" do
assert_accepts redirect_to('somewhere') { '/some/url' }, @controller
end

should "reject redirecting to a different url in a block" do
assert_rejects redirect_to('somewhere else') { '/some/other/url' }, @controller
end
end

context "a controller that doesn't redirect" do
setup do
@controller = build_response { render :text => 'hello' }
end

should "reject redirecting to a url" do
assert_rejects redirect_to('/some/url'), @controller
end
end

end

0 comments on commit 0fe568f

Please sign in to comment.