Asserts that the request was rendered with the appropriate template file or partials.
# 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 lib/action_controller/test_case.rb, line 62 62: def assert_template(options = {}, message = nil) 63: validate_request! 64: 65: case options 66: when NilClass, String, Symbol 67: options = options.to_s if Symbol === options 68: rendered = @templates 69: msg = build_message(message, 70: "expecting <?> but rendering with <?>", 71: options, rendered.keys.join(', ')) 72: assert_block(msg) do 73: if options.nil? 74: @templates.blank? 75: else 76: rendered.any? { |t,num| t.match(options) } 77: end 78: end 79: when Hash 80: if expected_partial = options[:partial] 81: if expected_locals = options[:locals] 82: actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')] 83: expected_locals.each_pair do |k,v| 84: assert_equal(v, actual_locals[k]) 85: end 86: elsif expected_count = options[:count] 87: actual_count = @partials[expected_partial] 88: msg = build_message(message, 89: "expecting ? to be rendered ? time(s) but rendered ? time(s)", 90: expected_partial, expected_count, actual_count) 91: assert(actual_count == expected_count.to_i, msg) 92: elsif options.key?(:layout) 93: msg = build_message(message, 94: "expecting layout <?> but action rendered <?>", 95: expected_layout, @layouts.keys) 96: 97: case layout = options[:layout] 98: when String 99: assert(@layouts.include?(expected_layout), msg) 100: when Regexp 101: assert(@layouts.any? {|l| l =~ layout }, msg) 102: when nil 103: assert(@layouts.empty?, msg) 104: end 105: else 106: msg = build_message(message, 107: "expecting partial <?> but action rendered <?>", 108: options[:partial], @partials.keys) 109: assert(@partials.include?(expected_partial), msg) 110: end 111: else 112: assert @partials.empty?, 113: "Expected no partials to be rendered" 114: end 115: end 116: end
# File lib/action_controller/test_case.rb, line 14 14: def setup_subscriptions 15: @partials = Hash.new(0) 16: @templates = Hash.new(0) 17: @layouts = Hash.new(0) 18: 19: ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload| 20: path = payload[:layout] 21: @layouts[path] += 1 22: end 23: 24: ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload| 25: path = payload[:virtual_path] 26: next unless path 27: partial = path =~ /^.*\/_[^\/]*$/ 28: if partial 29: @partials[path] += 1 30: @partials[path.split("/").last] += 1 31: @templates[path] += 1 32: else 33: @templates[path] += 1 34: end 35: end 36: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.