Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 95 lines (79 sloc) 2.368 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
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)
          unless expected_layout.nil?
            @expected_layout = expected_layout.to_s
          end
        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?
          if @expected_layout.nil?
            true
          else
            rendered_layouts.include?(@expected_layout)
          end
        end

        def rendered_layouts
          recorded_layouts.keys.compact.map { |layout| layout.sub(%r{^layouts/}, '') }
        end

        def recorded_layouts
          if @context
            @context.instance_variable_get('@layouts')
          else
            {}
          end
        end

        def expectation
          "to #{description}"
        end

        def result
          if rendered_with_layout?
            "rendered with " + rendered_layouts.map(&:inspect).join(", ")
          else
            "rendered without a layout"
          end
        end
      end
    end
  end
end
Something went wrong with that request. Please try again.