Permalink
Browse files

Converted should_render_with_layout/should_render_without_layout to u…

…se a matcher
  • Loading branch information...
1 parent 048c4d1 commit 4a788344022d1732e04bd4b3d11ff7ec36467597 @jferris jferris committed Feb 4, 2009
@@ -1,5 +1,4 @@
Unfinished:
-should_render_with_layout should_render_without_layout
should_respond_with
should_respond_with_content_type
should_return_from_session
@@ -11,6 +10,8 @@ should_not_assign_to
should_filter_params
should_not_set_the_flash
should_set_the_flash_to
+should_render_with_layout
+should_render_without_layout
Won't implement:
should_be_restful
@@ -223,17 +223,14 @@ def should_render_template(template)
#
# should_render_with_layout 'special'
def should_render_with_layout(expected_layout = 'application')
+ matcher = render_with_layout(expected_layout)
if expected_layout
- should "render with #{expected_layout.inspect} layout" do
- response_layout = @response.layout.blank? ? "" : @response.layout.split('/').last
- assert_equal expected_layout.to_s,
- response_layout,
- "Expected to render with layout #{expected_layout} but was rendered with #{response_layout}"
+ should matcher.description do
+ assert_accepts matcher, @controller
end
else
should "render without layout" do
- assert_nil @response.layout,
- "Expected no layout, but was rendered using #{@response.layout}"
+ assert_rejects matcher, @controller
end
end
end
@@ -1,6 +1,7 @@
require 'shoulda/controller/matchers/assign_to_matcher'
require 'shoulda/controller/matchers/filter_param_matcher'
require 'shoulda/controller/matchers/set_the_flash_matcher'
+require 'shoulda/controller/matchers/render_with_layout_matcher'
module Shoulda # :nodoc:
module Controller # :nodoc:
@@ -0,0 +1,81 @@
+module Shoulda # :nodoc:
+ module Controller # :nodoc:
+ module Matchers
+
+ # Ensures that the controller rendered with the given layout.
+ #
+ # Example:
+ #
+ # it { should render_with_layout }
+ # it { should render_with_layout(:special) }
+ # it { should_not render_with_layout }
+ def render_with_layout(layout = nil)
+ RenderWithLayout.new(layout)
+ end
+
+ class RenderWithLayout # :nodoc:
+
+ def initialize(layout)
+ @layout = layout.to_s unless layout.nil?
+ end
+
+ def matches?(controller)
+ @controller = controller
+ rendered_with_layout? && rendered_with_expected_layout?
+ end
+
+ def failure_message
+ "Expected #{expectation}, but #{result}"
+ end
+
+ def negative_failure_message
+ "Did not expect #{expectation}, but #{result}"
+ end
+
+ def description
+ description = "render with "
+ if @layout.nil?
+ description << "a layout"
+ else
+ description << "the #{@layout.inspect} layout"
+ end
+ description
+ end
+
+ private
+
+ def rendered_with_layout?
+ !layout.blank?
+ end
+
+ def rendered_with_expected_layout?
+ return true if @layout.nil?
+ layout == @layout
+ end
+
+ def layout
+ layout = @controller.response.layout
+ if layout.nil?
+ nil
+ else
+ layout.split('/').last
+ end
+ end
+
+ def expectation
+ "to #{description}"
+ end
+
+ def result
+ if rendered_with_layout?
+ "rendered with the #{layout.inspect} layout"
+ else
+ "rendered without a layout"
+ end
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,33 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
+
+class RenderWithLayoutMatcherTest < Test::Unit::TestCase # :nodoc:
+
+ context "a controller that renders with a layout" do
+ setup do
+ @controller = build_response { render :layout => 'wide' }
+ end
+
+ should "accept rendering with any layout" do
+ assert_accepts render_with_layout, @controller
+ end
+
+ should "accept rendering with that layout" do
+ assert_accepts render_with_layout(:wide), @controller
+ end
+
+ should "reject rendering with another layout" do
+ assert_rejects render_with_layout(:other), @controller
+ end
+ end
+
+ context "a controller that renders without a layout" do
+ setup do
+ @controller = build_response { render :layout => false }
+ end
+
+ should "reject rendering with a layout" do
+ assert_rejects render_with_layout, @controller
+ end
+ end
+
+end

0 comments on commit 4a78834

Please sign in to comment.