Module: ActionController::TemplateAssertions
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Defined in: | actionpack/lib/action_controller/test_case.rb |
Constant Summary
-
RENDER_TEMPLATE_INSTANCE_VARIABLES =
# File 'actionpack/lib/action_controller/test_case.rb', line 18%w{partials templates layouts files}.freeze
Class Method Summary
::ActiveSupport::Concern - Extended
Instance Method Summary
-
#assert_template(options = {}, message = nil)
Asserts that the request was rendered with the appropriate template file or partials.
- #process(*args)
- #reset_template_assertion
- #setup_subscriptions
- #teardown_subscriptions
DSL Calls
included
[ GitHub ]13 14 15 16
# File 'actionpack/lib/action_controller/test_case.rb', line 13
included do setup :setup_subscriptions teardown :teardown_subscriptions end
Instance Method Details
#assert_template(options = {}, message = nil)
Asserts that the request was rendered with the appropriate template file or partials.
# 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 layout 'admin' was rendered
assert_template layout: 'admin'
assert_template layout: 'layouts/admin'
assert_template layout: :admin
# assert that no layout was rendered
assert_template layout: nil
assert_template layout: false
# assert that the "_customer" partial was rendered twice
assert_template partial: '_customer', count: 2
# assert that no partials were rendered
assert_template partial: false
# assert that a file was rendered
assert_template file: "README.rdoc"
# assert that no file was rendered
assert_template file: nil
assert_template file: 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 114
def assert_template( = {}, = nil) # Force body to be read in case the template is being streamed. response.body case when NilClass, Regexp, String, Symbol = .to_s if Symbol === rendered = @_templates msg = || sprintf("expecting <%s> but rendering with <%s>", .inspect, rendered.keys) matches_template = case when String ! .empty? && rendered.any? do |t, num| = .split(File::SEPARATOR) t_splited = t.split(File::SEPARATOR) t_splited.last( .size) == end when Regexp rendered.any? { |t,num| t.match( ) } when NilClass rendered.blank? end assert matches_template, msg when Hash .assert_valid_keys(:layout, :partial, :locals, :count, :file) if .key?(:layout) expected_layout = [:layout] msg = || 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 [:file] assert_includes @_files.keys, [:file] elsif .key?(:file) assert @_files.blank?, "expected no files but #{@_files.keys} was rendered" end if expected_partial = [:partial] if expected_locals = [: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, [:locals]), msg) else warn "the :locals option to #assert_template is only supported in a ActionView::TestCase" end elsif expected_count = [:count] actual_count = @_partials[expected_partial] msg = || 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 = || sprintf("expecting partial <%s> but action rendered <%s>", [:partial], @_partials.keys) assert_includes @_partials, expected_partial, msg end elsif .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
#process(*args)
[ GitHub ]# File 'actionpack/lib/action_controller/test_case.rb', line 65
def process(*args) reset_template_assertion super end
#reset_template_assertion
[ GitHub ]# File 'actionpack/lib/action_controller/test_case.rb', line 70
def reset_template_assertion RENDER_TEMPLATE_INSTANCE_VARIABLES.each do |instance_variable| ivar_name = "@_#{instance_variable}" if instance_variable_defined?(ivar_name) instance_variable_get(ivar_name).clear end end end
#setup_subscriptions
[ GitHub ]# File 'actionpack/lib/action_controller/test_case.rb', line 20
def setup_subscriptions RENDER_TEMPLATE_INSTANCE_VARIABLES.each do |instance_variable| instance_variable_set("@_#{instance_variable}", Hash.new(0)) end @_subscribers = [] @_subscribers << ActiveSupport::Notifications.subscribe("render_template.action_view") do |_name, _start, _finish, _id, payload| path = payload[:layout] if path @_layouts[path] += 1 if path =~ /^layouts\/(.*)/ @_layouts[$1] += 1 end end end @_subscribers << ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload| if virtual_path = payload[:virtual_path] partial = virtual_path =~ /^.*\/_[^\/]*$/ if partial @_partials[virtual_path] += 1 @_partials[virtual_path.split("/").last] += 1 end @_templates[virtual_path] += 1 else path = payload[:identifier] if path @_files[path] += 1 @_files[path.split("/").last] += 1 end end end end
#teardown_subscriptions
[ GitHub ]# File 'actionpack/lib/action_controller/test_case.rb', line 57
def teardown_subscriptions return unless defined?(@_subscribers) @_subscribers.each do |subscriber| ActiveSupport::Notifications.unsubscribe(subscriber) end end