aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2012-12-15 20:20:34 +0100
committerYves Senn <yves.senn@gmail.com>2013-02-04 14:46:50 +0100
commitc21ab338cb9e38a0bfa51ac8cf4d70285f03c7b2 (patch)
treefa41ad8f396f707c407b9ce716f640adecaf6970
parente16110c4b86e7ba5eb7d4accf8871e98122a10e5 (diff)
downloadrails-c21ab338cb9e38a0bfa51ac8cf4d70285f03c7b2.tar.gz
rails-c21ab338cb9e38a0bfa51ac8cf4d70285f03c7b2.tar.bz2
rails-c21ab338cb9e38a0bfa51ac8cf4d70285f03c7b2.zip
descriptive `assert_template` error when partial wasn't rendered
When `assert_template` is used with the :locals option, and the partial was not rendered, a method_missing error was raised. This changes first checks, if the partial actually was rendered and raises a descriptive error.
-rw-r--r--actionpack/lib/action_controller/test_case.rb3
-rw-r--r--actionpack/lib/action_view/test_case.rb4
-rw-r--r--actionpack/test/template/test_case_test.rb9
3 files changed, 16 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb
index 5ae5dd331a..9a7f1b5c3a 100644
--- a/actionpack/lib/action_controller/test_case.rb
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -127,6 +127,9 @@ module ActionController
if expected_locals = options[:locals]
if defined?(@_rendered_views)
view = expected_partial.to_s.sub(/^_/,'')
+ 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)]
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index 4479da5bc4..1f89e51c66 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -134,6 +134,10 @@ module ActionView
@rendered_views[view]
end
+ def rendered_views
+ @rendered_views.keys
+ end
+
def view_rendered?(view, expected_locals)
locals_for(view).any? do |actual_locals|
expected_locals.all? {|key, value| value == actual_locals[key] }
diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb
index c7231d9cd5..c8441b6894 100644
--- a/actionpack/test/template/test_case_test.rb
+++ b/actionpack/test/template/test_case_test.rb
@@ -329,6 +329,15 @@ module ActionView
assert_template partial: '_partial', locals: { 'second' => '2' }
end
+ test 'raises descriptive error message when template was not rendered' do
+ controller.controller_path = "test"
+ render(template: "test/hello_world_with_partial")
+ e = assert_raise ActiveSupport::TestCase::Assertion do
+ assert_template partial: 'i_was_never_rendered', locals: { 'did_not' => 'happen' }
+ end
+ assert_match "i_was_never_rendered to be rendered but it was not.", e.message
+ assert_match 'Expected ["/test/partial"] to include "i_was_never_rendered"', e.message
+ end
end
module AHelperWithInitialize