Skip to content
Browse files

add a :class option to should_assign_to which checks that the instanc…

…e vars are of the correct class
  • Loading branch information...
1 parent 2c41a56 commit a62c376628d01773899305b05e9be03f28d79e9f @mjankowski mjankowski committed with Tammer Saleh Sep 20, 2008
Showing with 13 additions and 1 deletion.
  1. +8 −0 lib/shoulda/controller/macros.rb
  2. +5 −1 test/functional/posts_controller_test.rb
View
8 lib/shoulda/controller/macros.rb
@@ -99,13 +99,21 @@ def should_not_set_the_flash
# Macro that creates a test asserting that the controller assigned to
# each of the named instance variable(s).
#
+ # Options:
+ # * <tt>:class</tt> - 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
View
6 test/functional/posts_controller_test.rb
@@ -64,7 +64,11 @@ def setup
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

0 comments on commit a62c376

Please sign in to comment.
Something went wrong with that request. Please try again.