Methods
A
P
S
T
Instance Public methods
assert_template(options = {}, message = nil)

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
def assert_template(options = {}, message = nil)
  validate_request!

  case options
  when NilClass, String, Symbol
    options = options.to_s if Symbol === options
    rendered = @templates
    msg = build_message(message,
            "expecting <?> but rendering with <?>",
            options, rendered.keys.join(', '))
    assert_block(msg) do
      if options.nil?
        @templates.blank?
      else
        rendered.any? { |t,num| t.match(options) }
      end
    end
  when Hash
    if expected_partial = options[:partial]
      if expected_locals = options[:locals]
        actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')]
        expected_locals.each_pair do |k,v|
          assert_equal(v, actual_locals[k])
        end
      elsif expected_count = options[:count]
        actual_count = @partials[expected_partial]
        msg = build_message(message,
                "expecting ? to be rendered ? time(s) but rendered ? time(s)",
                 expected_partial, expected_count, actual_count)
        assert(actual_count == expected_count.to_i, msg)
      elsif options.key?(:layout)
        msg = build_message(message,
                "expecting layout <?> but action rendered <?>",
                expected_layout, @layouts.keys)

        case layout = options[:layout]
        when String
          assert(@layouts.include?(expected_layout), msg)
        when Regexp
          assert(@layouts.any? {|l| l =~ layout }, msg)
        when nil
          assert(@layouts.empty?, msg)
        end
      else
        msg = build_message(message,
                "expecting partial <?> but action rendered <?>",
                options[:partial], @partials.keys)
        assert(@partials.include?(expected_partial), msg)
      end
    else
      assert @partials.empty?,
        "Expected no partials to be rendered"
    end
  end
end
process(*args)
# File actionpack/lib/action_controller/test_case.rb, line 45
def process(*args)
  @partials = Hash.new(0)
  @templates = Hash.new(0)
  @layouts = Hash.new(0)
  super
end
setup_subscriptions()
# File actionpack/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]
    @layouts[path] += 1
  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()
# File actionpack/lib/action_controller/test_case.rb, line 40
def teardown_subscriptions
  ActiveSupport::Notifications.unsubscribe("render_template.action_view")
  ActiveSupport::Notifications.unsubscribe("!render_template.action_view")
end