-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
5 changed files
with
113 additions
and
4 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
54 changes: 54 additions & 0 deletions
54
lib/shoulda/action_controller/matchers/render_template_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,54 @@ | ||
module Shoulda # :nodoc: | ||
module ActionController # :nodoc: | ||
module Matchers | ||
|
||
# Ensures a controller rendered the given template. | ||
# | ||
# Example: | ||
# | ||
# it { should render_template(:show) } | ||
def render_template(template) | ||
RenderTemplateMatcher.new(template, self) | ||
end | ||
|
||
class RenderTemplateMatcher # :nodoc: | ||
|
||
def initialize(template, context) | ||
@template = template | ||
@context = context | ||
end | ||
|
||
def matches?(controller) | ||
@controller = controller | ||
renders_template? | ||
end | ||
|
||
attr_reader :failure_message, :negative_failure_message | ||
|
||
def description | ||
"render template #{@template}" | ||
end | ||
|
||
def in_context(context) | ||
@context = context | ||
self | ||
end | ||
|
||
private | ||
|
||
def renders_template? | ||
begin | ||
@context.send(:assert_template, @template) | ||
@negative_failure_message = "Didn't expect to render #{@template}" | ||
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 RenderTemplateMatcherTest < ActionController::TestCase # :nodoc: | ||
|
||
context "a controller that renders a template" do | ||
setup do | ||
@controller = build_response(:action => 'show') { render } | ||
end | ||
|
||
should "accept rendering that template" do | ||
assert_accepts render_template(:show), @controller | ||
end | ||
|
||
should "reject rendering a different template" do | ||
assert_rejects render_template(:index), @controller | ||
end | ||
|
||
should "accept rendering that template in the given context" do | ||
assert_accepts self.class.render_template(:show).in_context(self), @controller | ||
end | ||
|
||
should "reject rendering a different template in the given context" do | ||
assert_rejects self.class.render_template(:index).in_context(self), @controller | ||
end | ||
end | ||
|
||
context "a controller that doesn't render a template" do | ||
setup do | ||
@controller = build_response { render :nothing => true } | ||
end | ||
|
||
should "reject rendering a template" do | ||
assert_rejects render_template(:show), @controller | ||
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
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