Methods
Instance Public methods
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 "_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 }
# File actionpack/lib/action_controller/test_case.rb, line 71 71: def assert_template(options = {}, message = nil) 72: validate_request! 73: 74: case options 75: when NilClass, String, Symbol 76: options = options.to_s if Symbol === options 77: rendered = @templates 78: msg = build_message(message, 79: "expecting <?> but rendering with <?>", 80: options, rendered.keys.join(', ')) 81: assert_block(msg) do 82: if options 83: rendered.any? { |t,num| t.match(options) } 84: else 85: @templates.blank? 86: end 87: end 88: when Hash 89: if expected_layout = options[:layout] 90: msg = build_message(message, 91: "expecting layout <?> but action rendered <?>", 92: expected_layout, @layouts.keys) 93: 94: case expected_layout 95: when String 96: assert(@layouts.keys.include?(expected_layout), msg) 97: when Regexp 98: assert(@layouts.keys.any? {|l| l =~ expected_layout }, msg) 99: when nil 100: assert(@layouts.empty?, msg) 101: end 102: end 103: 104: if expected_partial = options[:partial] 105: if expected_locals = options[:locals] 106: actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')] 107: expected_locals.each_pair do |k,v| 108: assert_equal(v, actual_locals[k]) 109: end 110: elsif expected_count = options[:count] 111: actual_count = @partials[expected_partial] 112: msg = build_message(message, 113: "expecting ? to be rendered ? time(s) but rendered ? time(s)", 114: expected_partial, expected_count, actual_count) 115: assert(actual_count == expected_count.to_i, msg) 116: else 117: msg = build_message(message, 118: "expecting partial <?> but action rendered <?>", 119: options[:partial], @partials.keys) 120: assert(@partials.include?(expected_partial), msg) 121: end 122: else 123: assert @partials.empty?, 124: "Expected no partials to be rendered" 125: end 126: end 127: end
# File actionpack/lib/action_controller/test_case.rb, line 16 16: def setup_subscriptions 17: @partials = Hash.new(0) 18: @templates = Hash.new(0) 19: @layouts = Hash.new(0) 20: 21: ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload| 22: path = payload[:layout] 23: @layouts[path] += 1 24: end 25: 26: ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload| 27: path = payload[:virtual_path] 28: next unless path 29: partial = path =~ /^.*\/_[^\/]*$/ 30: if partial 31: @partials[path] += 1 32: @partials[path.split("/").last] += 1 33: @templates[path] += 1 34: else 35: @templates[path] += 1 36: end 37: end 38: end