aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorDavid Chelimsky <dchelimsky@gmail.com>2010-06-22 01:40:22 -0500
committerJosé Valim <jose.valim@gmail.com>2010-06-22 14:51:22 +0200
commit0576ec4ddd5f806c6ae98184d28391fc239db1ef (patch)
treea0563ebe3f34e2f99c25554c2e57d38d8b37067c /actionpack
parent02399a1184d0f87b14af461fab731120d92c5ad8 (diff)
downloadrails-0576ec4ddd5f806c6ae98184d28391fc239db1ef.tar.gz
rails-0576ec4ddd5f806c6ae98184d28391fc239db1ef.tar.bz2
rails-0576ec4ddd5f806c6ae98184d28391fc239db1ef.zip
Add support for specifying locals in view tests with assert template [#4927 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/test_case.rb16
-rw-r--r--actionpack/lib/action_view/test_case.rb15
-rw-r--r--actionpack/test/template/test_case_test.rb18
3 files changed, 44 insertions, 5 deletions
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb
index 7f9eb2cfd1..26a385011f 100644
--- a/actionpack/lib/action_controller/test_case.rb
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -40,7 +40,7 @@ module ActionController
ActiveSupport::Notifications.unsubscribe("!render_template.action_view")
end
- # Asserts that the request was rendered with the appropriate template file or partials
+ # Asserts that the request was rendered with the appropriate template file or partials.
#
# ==== Examples
#
@@ -53,6 +53,12 @@ module ActionController
# # 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 }
+ #
def assert_template(options = {}, message = nil)
validate_request!
@@ -72,9 +78,13 @@ module ActionController
end
when Hash
if expected_partial = options[:partial]
- if expected_count = options[:count]
+ if expected_locals = options[:locals]
+ actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')]
+ expected_locals.each_pair do |k,v|
+ assert_equal(v, actual_locals[k])
+ end
+ elsif expected_count = options[:count]
actual_count = @partials[expected_partial]
- # actual_count = found.nil? ? 0 : found[1]
msg = build_message(message,
"expecting ? to be rendered ? time(s) but rendered ? time(s)",
expected_partial, expected_count, actual_count)
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index b698b4cfec..fcfbb92157 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -103,6 +103,10 @@ module ActionView
output
end
+ def locals
+ @locals ||= {}
+ end
+
included do
setup :setup_with_controller
end
@@ -132,12 +136,23 @@ module ActionView
end
end
+ module Locals
+ attr_accessor :locals
+
+ def _render_partial(options)
+ locals[options[:partial]] = options[:locals]
+ super(options)
+ end
+ end
+
def _view
@_view ||= begin
view = ActionView::Base.new(ActionController::Base.view_paths, _assigns, @controller)
view.singleton_class.send :include, _helpers
view.singleton_class.send :include, @controller._router.url_helpers
view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"
+ view.extend(Locals)
+ view.locals = self.locals
view.output_buffer = self.output_buffer
view
end
diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb
index c365aec841..d86dc7b185 100644
--- a/actionpack/test/template/test_case_test.rb
+++ b/actionpack/test/template/test_case_test.rb
@@ -220,10 +220,24 @@ module ActionView
end
class RenderTemplateTest < ActionView::TestCase
- test "render template" do
+ test "render template supports specifying partials" do
controller.controller_path = "test"
render(:template => "test/calling_partial_with_layout")
- assert_template "partial_for_use_in_layout"
+ assert_template :partial => "_partial_for_use_in_layout"
+ end
+
+ test "render template supports specifying locals (passing)" do
+ controller.controller_path = "test"
+ render(:template => "test/calling_partial_with_layout")
+ assert_template :partial => "_partial_for_use_in_layout", :locals => { :name => "David" }
+ end
+
+ test "render template supports specifying locals (failing)" do
+ controller.controller_path = "test"
+ render(:template => "test/calling_partial_with_layout")
+ assert_raise Test::Unit::AssertionFailedError, /Somebody else.*David/m do
+ assert_template :partial => "_partial_for_use_in_layout", :locals => { :name => "Somebody Else" }
+ end
end
end
end