Created
March 13, 2013 16:11
-
-
Save senny/5153632 to your computer and use it in GitHub Desktop.
realy...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def assert_template(options = {}, message = nil) | |
# Force body to be read in case the template is being streamed. | |
response.body | |
case options | |
when NilClass, Regexp, String, Symbol | |
options = options.to_s if Symbol === options | |
rendered = @_templates | |
msg = message || sprintf("expecting <%s> but rendering with <%s>", | |
options.inspect, rendered.keys) | |
matches_template = | |
case options | |
when String | |
!options.empty? && rendered.any? do |t, num| | |
options_splited = options.split(File::SEPARATOR) | |
t_splited = t.split(File::SEPARATOR) | |
t_splited.last(options_splited.size) == options_splited | |
end | |
when Regexp | |
rendered.any? { |t,num| t.match(options) } | |
when NilClass | |
rendered.blank? | |
end | |
assert matches_template, msg | |
when Hash | |
options.assert_valid_keys(:layout, :partial, :locals, :count, :file) | |
if options.key?(:layout) | |
expected_layout = options[:layout] | |
msg = message || sprintf("expecting layout <%s> but action rendered <%s>", | |
expected_layout, @_layouts.keys) | |
case expected_layout | |
when String, Symbol | |
assert_includes @_layouts.keys, expected_layout.to_s, msg | |
when Regexp | |
assert(@_layouts.keys.any? {|l| l =~ expected_layout }, msg) | |
when nil, false | |
assert(@_layouts.empty?, msg) | |
end | |
end | |
if options[:file] | |
assert_includes @_files.keys, options[:file] | |
end | |
if expected_partial = options[:partial] | |
if expected_locals = options[:locals] | |
if defined?(@_rendered_views) | |
view = expected_partial.to_s.sub(/^_/, '').sub(/\/_(?=[^\/]+\z)/, '/') | |
partial_was_not_rendered_msg = "expected %s to be rendered but it was not." % view | |
assert_includes @_rendered_views.rendered_views, view, partial_was_not_rendered_msg | |
msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial, | |
expected_locals, | |
@_rendered_views.locals_for(view)] | |
assert(@_rendered_views.view_rendered?(view, options[:locals]), msg) | |
else | |
warn "the :locals option to #assert_template is only supported in a ActionView::TestCase" | |
end | |
elsif expected_count = options[:count] | |
actual_count = @_partials[expected_partial] | |
msg = message || sprintf("expecting %s to be rendered %s time(s) but rendered %s time(s)", | |
expected_partial, expected_count, actual_count) | |
assert(actual_count == expected_count.to_i, msg) | |
else | |
msg = message || sprintf("expecting partial <%s> but action rendered <%s>", | |
options[:partial], @_partials.keys) | |
assert_includes @_partials, expected_partial, msg | |
end | |
elsif options.key?(:partial) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment