Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

assert_template matches against Regexp #5375

Merged
merged 1 commit into from

2 participants

@nertzy

This allows for more strict template assertions, while maintaining backward compatibility.

For example, if you use assert_template("foo/bar") and "foo/bar/baz" was rendered, the test passes.

But if you use assert_template(%r{\Afoo/bar\Z}), you will catch that a different template was rendered.

Also, if you passed an unsupported argument to assert_template() in the past, it would silently succeed. Now it raises an ArgumentError.

@nertzy nertzy assert_template matches against Regexp
This allows for more strict template assertions,
while maintaining backward compatibility.

For example, if you use assert_template("foo/bar")
and "foo/bar/baz" was rendered, the test passes.

But if you use assert_template(%r{\Afoo/bar\Z}),
you will catch that a different template was
rendered.

Also, if you passed an unsupported argument to
assert_template() in the past, it would silently
succeed. Now it raises an ArgumentError.
b142bd8
@tenderlove tenderlove merged commit fd22471 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 11, 2012
  1. @nertzy

    assert_template matches against Regexp

    nertzy authored
    This allows for more strict template assertions,
    while maintaining backward compatibility.
    
    For example, if you use assert_template("foo/bar")
    and "foo/bar/baz" was rendered, the test passes.
    
    But if you use assert_template(%r{\Afoo/bar\Z}),
    you will catch that a different template was
    rendered.
    
    Also, if you passed an unsupported argument to
    assert_template() in the past, it would silently
    succeed. Now it raises an ArgumentError.
This page is out of date. Refresh to see the latest.
View
9 actionpack/lib/action_controller/test_case.rb
@@ -56,6 +56,9 @@ def process(*args)
# # assert that the "new" view template was rendered
# assert_template "new"
#
+ # # assert that the exact template "admin/posts/new" was rendered
+ # assert_template %r{\Aadmin/posts/new\Z}
+ #
# # assert that the "_customer" partial was rendered twice
# assert_template :partial => '_customer', :count => 2
#
@@ -74,11 +77,11 @@ def assert_template(options = {}, message = nil)
response.body
case options
- when NilClass, String, Symbol
+ when NilClass, String, Symbol, Regexp
options = options.to_s if Symbol === options
rendered = @templates
msg = message || sprintf("expecting <%s> but rendering with <%s>",
- options, rendered.keys)
+ options.inspect, rendered.keys)
assert_block(msg) do
if options
rendered.any? { |t,num| t.match(options) }
@@ -121,6 +124,8 @@ def assert_template(options = {}, message = nil)
assert @partials.empty?,
"Expected no partials to be rendered"
end
+ else
+ raise ArgumentError, "assert_template only accepts a String, Symbol, Hash, Regexp, or nil"
end
end
end
View
6 actionpack/test/template/test_case_test.rb
@@ -277,6 +277,12 @@ def render_from_helper
end
class RenderTemplateTest < ActionView::TestCase
+ test "supports specifying templates with a Regexp" do
+ controller.controller_path = "fun"
+ render(:template => "fun/games/hello_world")
+ assert_template %r{\Afun/games/hello_world\Z}
+ end
+
test "supports specifying partials" do
controller.controller_path = "test"
render(:template => "test/calling_partial_with_layout")
Something went wrong with that request. Please try again.