File: render_with_layout_matcher.rb

package info (click to toggle)
libshoulda-ruby 2.10.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 880 kB
  • ctags: 726
  • sloc: ruby: 5,764; makefile: 6
file content (81 lines) | stat: -rw-r--r-- 1,832 bytes parent folder | download
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
module Shoulda # :nodoc:
  module ActionController # :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