Module: ActionController::TemplateAssertions
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/action_controller/test_case.rb
Instance Method Summary collapse
-
#assert_template(options = {}, message = nil) ⇒ Object
Asserts that the request was rendered with the appropriate template file or partials.
- #process(*args) ⇒ Object
- #setup_subscriptions ⇒ Object
- #teardown_subscriptions ⇒ Object
Instance Method Details
#assert_template(options = {}, message = nil) ⇒ Object
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
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 }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/action_controller/test_case.rb', line 95 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] 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) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/action_controller/test_case.rb', line 60 def process(*args) @_partials = Hash.new(0) @_templates = Hash.new(0) @_layouts = Hash.new(0) super end |
#setup_subscriptions ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/action_controller/test_case.rb', line 15 def setup_subscriptions @_partials = Hash.new(0) @_templates = Hash.new(0) @_layouts = Hash.new(0) @_files = Hash.new(0) 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 ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload| path = payload[:virtual_path] next unless path partial = path =~ /^.*\/_[^\/]*$/ if partial @_partials[path] += 1 @_partials[path.split("/").last] += 1 end @_templates[path] += 1 end ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload| next if payload[:virtual_path] # files don't have virtual path path = payload[:identifier] if path @_files[path] += 1 @_files[path.split("/").last] += 1 end end end |
#teardown_subscriptions ⇒ Object
55 56 57 58 |
# File 'lib/action_controller/test_case.rb', line 55 def teardown_subscriptions ActiveSupport::Notifications.unsubscribe("render_template.action_view") ActiveSupport::Notifications.unsubscribe("!render_template.action_view") end |