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 69 69: def assert_template(options = {}, message = nil) 70: validate_request! 71: 72: case options 73: when NilClass, String, Symbol 74: options = options.to_s if Symbol === options 75: rendered = @templates 76: msg = build_message(message, 77: "expecting <?> but rendering with <?>", 78: options, rendered.keys.join(', ')) 79: assert_block(msg) do 80: if options.nil? 81: @templates.blank? 82: else 83: rendered.any? { |t,num| t.match(options) } 84: end 85: end 86: when Hash 87: if expected_partial = options[:partial] 88: if expected_locals = options[:locals] 89: actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')] 90: expected_locals.each_pair do |k,v| 91: assert_equal(v, actual_locals[k]) 92: end 93: elsif expected_count = options[:count] 94: actual_count = @partials[expected_partial] 95: msg = build_message(message, 96: "expecting ? to be rendered ? time(s) but rendered ? time(s)", 97: expected_partial, expected_count, actual_count) 98: assert(actual_count == expected_count.to_i, msg) 99: elsif options.key?(:layout) 100: msg = build_message(message, 101: "expecting layout <?> but action rendered <?>", 102: expected_layout, @layouts.keys) 103: 104: case layout = options[:layout] 105: when String 106: assert(@layouts.include?(expected_layout), msg) 107: when Regexp 108: assert(@layouts.any? {|l| l =~ layout }, msg) 109: when nil 110: assert(@layouts.empty?, msg) 111: end 112: else 113: msg = build_message(message, 114: "expecting partial <?> but action rendered <?>", 115: options[:partial], @partials.keys) 116: assert(@partials.include?(expected_partial), msg) 117: end 118: else 119: assert @partials.empty?, 120: "Expected no partials to be rendered" 121: end 122: end 123: end
# File lib/action_controller/test_case.rb, line 43 43: def process(*args) 44: @partials = Hash.new(0) 45: @templates = Hash.new(0) 46: @layouts = Hash.new(0) 47: super 48: 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.