Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
lib/shoulda/action_controller/matchers/redirect_to_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |