Module: ActionController::TemplateAssertions
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/action_controller/test_case.rb
Instance Method Summary collapse
-
#assert_template(options = {}, message = nil) ⇒ Object
Asserts that the request was rendered with the appropriate template file or partials.
- #process(*args) ⇒ Object
- #setup_subscriptions ⇒ Object
- #teardown_subscriptions ⇒ Object
Instance Method Details
#assert_template(options = {}, message = nil) ⇒ Object
Asserts that the request was rendered with the appropriate template file or partials.
Examples
# assert that the "new" view template was rendered
assert_template "new"
# assert that the layout 'admin' was rendered
assert_template :layout => 'admin'
assert_template :layout => 'layouts/admin'
assert_template :layout => :admin
# assert that no layout was rendered
assert_template :layout => nil
assert_template :layout => false
# assert that the "_customer" partial was rendered twice
assert_template :partial => '_customer', :count => 2
# assert that no partials were rendered
assert_template :partial => false
In a view test case, you can also assert that specific locals are passed to partials:
# assert that the "_customer" partial was rendered with a specific object
assert_template :partial => '_customer', :locals => { :customer => @customer }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/action_controller/test_case.rb', line 85 def assert_template( = {}, = nil) validate_request! # Force body to be read in case the template is being streamed response.body case when NilClass, String, Symbol = .to_s if Symbol === rendered = @templates msg = (, "expecting <?> but rendering with <?>", , rendered.keys.join(', ')) assert_block(msg) do if rendered.any? { |t,num| t.match() } else @templates.blank? end end when Hash if .key?(:layout) expected_layout = [:layout] msg = (, "expecting layout <?> but action rendered <?>", expected_layout, @layouts.keys) case expected_layout when String, Symbol assert(@layouts.keys.include?(expected_layout.to_s), msg) when Regexp assert(@layouts.keys.any? {|l| l =~ expected_layout }, msg) when nil, false assert(@layouts.empty?, msg) end end if expected_partial = [:partial] if expected_locals = [:locals] if defined?(@locals) actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')] expected_locals.each_pair do |k,v| assert_equal(v, actual_locals[k]) end else warn "the :locals option to #assert_template is only supported in a ActionView::TestCase" end elsif expected_count = [:count] actual_count = @partials[expected_partial] msg = (, "expecting ? to be rendered ? time(s) but rendered ? time(s)", expected_partial, expected_count, actual_count) assert(actual_count == expected_count.to_i, msg) else msg = (, "expecting partial <?> but action rendered <?>", [:partial], @partials.keys) assert(@partials.include?(expected_partial), msg) end elsif .key?(:partial) assert @partials.empty?, "Expected no partials to be rendered" end end end |
#process(*args) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/action_controller/test_case.rb', line 50 def process(*args) @partials = Hash.new(0) @templates = Hash.new(0) @layouts = Hash.new(0) super end |
#setup_subscriptions ⇒ Object
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 |
# File 'lib/action_controller/test_case.rb', line 16 def setup_subscriptions @partials = Hash.new(0) @templates = Hash.new(0) @layouts = Hash.new(0) ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload| path = payload[:layout] if path @layouts[path] += 1 if path =~ /^layouts\/(.*)/ @layouts[$1] += 1 end end end ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload| path = payload[:virtual_path] next unless path partial = path =~ /^.*\/_[^\/]*$/ if partial @partials[path] += 1 @partials[path.split("/").last] += 1 @templates[path] += 1 else @templates[path] += 1 end end end |
#teardown_subscriptions ⇒ Object
45 46 47 48 |
# File 'lib/action_controller/test_case.rb', line 45 def teardown_subscriptions ActiveSupport::Notifications.unsubscribe("render_template.action_view") ActiveSupport::Notifications.unsubscribe("!render_template.action_view") end |