Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (79 sloc) 2.368 kB
56b0a04 @jferris Move into the Matchers namespace
jferris authored
1 module Shoulda # :nodoc:
2 module Matchers
3 module ActionController # :nodoc:
4
5 # Ensures that the controller rendered with the given layout.
6 #
7 # Example:
8 #
9 # it { should render_with_layout }
10 # it { should render_with_layout(:special) }
11 # it { should_not render_with_layout }
12 def render_with_layout(expected_layout = nil)
13 RenderWithLayoutMatcher.new(expected_layout).in_context(self)
14 end
15
16 class RenderWithLayoutMatcher # :nodoc:
17
18 def initialize(expected_layout)
73b5a46 @gabebw Cleanup.
gabebw authored
19 unless expected_layout.nil?
20 @expected_layout = expected_layout.to_s
21 end
56b0a04 @jferris Move into the Matchers namespace
jferris authored
22 end
23
24 # Used to provide access to layouts recorded by
25 # ActionController::TemplateAssertions in Rails 3
26 def in_context(context)
27 @context = context
28 self
29 end
30
31 def matches?(controller)
32 @controller = controller
33 rendered_with_layout? && rendered_with_expected_layout?
34 end
35
36 def failure_message
37 "Expected #{expectation}, but #{result}"
38 end
39
40 def negative_failure_message
41 "Did not expect #{expectation}, but #{result}"
42 end
43
44 def description
45 description = "render with "
46 if @expected_layout.nil?
47 description << "a layout"
48 else
49 description << "the #{@expected_layout.inspect} layout"
50 end
51 description
52 end
53
54 private
55
56 def rendered_with_layout?
57 !rendered_layouts.empty?
58 end
59
60 def rendered_with_expected_layout?
7b3d6d0 @gabebw Continue cleaning up matchers.
gabebw authored
61 if @expected_layout.nil?
62 true
63 else
64 rendered_layouts.include?(@expected_layout)
65 end
56b0a04 @jferris Move into the Matchers namespace
jferris authored
66 end
67
68 def rendered_layouts
e739a54 @elliterate Removed old Rails 2.x layout lookup.
elliterate authored
69 recorded_layouts.keys.compact.map { |layout| layout.sub(%r{^layouts/}, '') }
56b0a04 @jferris Move into the Matchers namespace
jferris authored
70 end
71
72 def recorded_layouts
73 if @context
74 @context.instance_variable_get('@layouts')
73b5a46 @gabebw Cleanup.
gabebw authored
75 else
76 {}
56b0a04 @jferris Move into the Matchers namespace
jferris authored
77 end
78 end
79
80 def expectation
81 "to #{description}"
82 end
83
84 def result
85 if rendered_with_layout?
7b3d6d0 @gabebw Continue cleaning up matchers.
gabebw authored
86 "rendered with " + rendered_layouts.map(&:inspect).join(", ")
56b0a04 @jferris Move into the Matchers namespace
jferris authored
87 else
88 "rendered without a layout"
89 end
90 end
91 end
92 end
93 end
94 end
Something went wrong with that request. Please try again.