instance method assert_template

Ruby on Rails 2.3.18

Since v2.2.3 Last seen in v2.3.18

Available in: v2.2.3 v2.3.18

Signature

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 "new" view template was rendered with Symbol
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

Parameters

options opt = {}
message opt = nil
Source
# File actionpack/lib/action_controller/assertions/response_assertions.rb, line 99
      def assert_template(options = {}, message = nil)
        clean_backtrace do
          case options
           when NilClass, String, Symbol
            rendered = @response.rendered[:template].to_s
            msg = build_message(message,
                    "expecting <?> but rendering with <?>",
                    options, rendered)
            assert_block(msg) do
              if options.nil?
                @response.rendered[:template].blank?
              else
                rendered.to_s.match(options.to_s)
              end
            end
          when Hash
            if expected_partial = options[:partial]
              partials = @response.rendered[:partials]
              if expected_count = options[:count]
                found = partials.detect { |p, _| p.to_s.match(expected_partial) }
                actual_count = found.nil? ? 0 : found.second
                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)
              else
                msg = build_message(message,
                        "expecting partial <?> but action rendered <?>",
                        options[:partial], partials.keys)
                assert(partials.keys.any? { |p| p.to_s.match(expected_partial) }, msg)
              end
            else
              assert @response.rendered[:partials].empty?,
                "Expected no partials to be rendered"
            end
          else
            raise ArgumentError  
          end
        end
      end

Defined in actionpack/lib/action_controller/assertions/response_assertions.rb line 99 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionController::Assertions::ResponseAssertions

Type at least 2 characters to search.

↑↓ navigate · open · esc close