This project is archived and is in readonly mode.

#5247 open
Mike Gehard

[PATCH] assert_template wrong behavior for testing layout used to render template?

Reported by Mike Gehard | July 30th, 2010 @ 05:53 AM | in 3.0.6

I am wondering why testing that a specific layout was used to render a template is dependent on you first testing that a partial is being rendered. From the code:

  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

Notice that the test for the "layout" param (elsif options.key?(:layout)) is nested within the check to see if you are testing for partials (if expected_partial = options[:partial]).

If I assume that this is correct behavior, which I don't think it should be, even if I pass in :partial => "something" then I get an error that says "undefined local variable expected_layout" because no where in assert_template does that variable get assigned.

I have looked through master and have found some tests that seem to be testing this but I am not sure that they are working properly because if you don't pass in the :partial option then it ignores the three assertions that test the @layouts collection.

Patches upcoming...

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages