2010-12-15 17:34:19 -05:00
|
|
|
module Shoulda # :nodoc:
|
|
|
|
module Matchers
|
|
|
|
module ActionController # :nodoc:
|
|
|
|
|
|
|
|
# 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(expected_layout = nil)
|
|
|
|
RenderWithLayoutMatcher.new(expected_layout).in_context(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
class RenderWithLayoutMatcher # :nodoc:
|
|
|
|
|
|
|
|
def initialize(expected_layout)
|
2012-05-07 11:07:53 -04:00
|
|
|
unless expected_layout.nil?
|
|
|
|
@expected_layout = expected_layout.to_s
|
|
|
|
end
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Used to provide access to layouts recorded by
|
|
|
|
# ActionController::TemplateAssertions in Rails 3
|
|
|
|
def in_context(context)
|
|
|
|
@context = context
|
|
|
|
self
|
|
|
|
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 @expected_layout.nil?
|
|
|
|
description << "a layout"
|
|
|
|
else
|
|
|
|
description << "the #{@expected_layout.inspect} layout"
|
|
|
|
end
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def rendered_with_layout?
|
|
|
|
!rendered_layouts.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def rendered_with_expected_layout?
|
2012-03-30 11:22:31 -04:00
|
|
|
if @expected_layout.nil?
|
|
|
|
true
|
|
|
|
else
|
|
|
|
rendered_layouts.include?(@expected_layout)
|
|
|
|
end
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rendered_layouts
|
2012-05-07 11:07:53 -04:00
|
|
|
if recorded_layouts.size > 0
|
2010-12-15 17:34:19 -05:00
|
|
|
recorded_layouts.keys.compact.map { |layout| layout.sub(%r{^layouts/}, '') }
|
|
|
|
else
|
|
|
|
layout = @controller.response.layout
|
|
|
|
if layout.nil?
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
[layout.split('/').last]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def recorded_layouts
|
|
|
|
if @context
|
|
|
|
@context.instance_variable_get('@layouts')
|
2012-05-07 11:07:53 -04:00
|
|
|
else
|
|
|
|
{}
|
2010-12-15 17:34:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expectation
|
|
|
|
"to #{description}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def result
|
|
|
|
if rendered_with_layout?
|
2012-03-30 11:22:31 -04:00
|
|
|
"rendered with " + rendered_layouts.map(&:inspect).join(", ")
|
2010-12-15 17:34:19 -05:00
|
|
|
else
|
|
|
|
"rendered without a layout"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|