From a62c376628d01773899305b05e9be03f28d79e9f Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Sat, 20 Sep 2008 15:11:57 -0400 Subject: [PATCH] add a :class option to should_assign_to which checks that the instance vars are of the correct class --- lib/shoulda/controller/macros.rb | 8 ++++++++ test/functional/posts_controller_test.rb | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/shoulda/controller/macros.rb b/lib/shoulda/controller/macros.rb index b94825c4..372ef27e 100644 --- a/lib/shoulda/controller/macros.rb +++ b/lib/shoulda/controller/macros.rb @@ -99,13 +99,21 @@ module ThoughtBot # :nodoc: # Macro that creates a test asserting that the controller assigned to # each of the named instance variable(s). # + # Options: + # * :class - The expected class of the instance variable being checked. + # # Example: # # should_assign_to :user, :posts + # should_assign_to :user, :class => User def should_assign_to(*names) + opts = names.extract_options! names.each do |name| should "assign @#{name}" do assert assigns(name.to_sym), "The action isn't assigning to @#{name}" + unless opts[:class].nil? + assert_kind_of opts[:class], assigns(name.to_sym) + end end end end diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index f567cac2..75b214e7 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -64,7 +64,11 @@ class PostsControllerTest < Test::Unit::TestCase get :index, :user_id => users(:first) end should_respond_with :success - should_assign_to :user, :posts + should_assign_to :user, :class => User + should_fail do + should_assign_to :user, :class => Post + end + should_assign_to :posts should_not_assign_to :foo, :bar end