1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

add a :class option to should_assign_to which checks that the instance vars are of the correct class

This commit is contained in:
Matt Jankowski 2008-09-20 15:11:57 -04:00 committed by Tammer Saleh
parent 2c41a563f9
commit a62c376628
2 changed files with 13 additions and 1 deletions

View file

@ -99,13 +99,21 @@ module ThoughtBot # :nodoc:
# Macro that creates a test asserting that the controller assigned to # Macro that creates a test asserting that the controller assigned to
# each of the named instance variable(s). # each of the named instance variable(s).
# #
# Options:
# * <tt>:class</tt> - The expected class of the instance variable being checked.
#
# Example: # Example:
# #
# should_assign_to :user, :posts # should_assign_to :user, :posts
# should_assign_to :user, :class => User
def should_assign_to(*names) def should_assign_to(*names)
opts = names.extract_options!
names.each do |name| names.each do |name|
should "assign @#{name}" do should "assign @#{name}" do
assert assigns(name.to_sym), "The action isn't assigning to @#{name}" 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 end
end end

View file

@ -64,7 +64,11 @@ class PostsControllerTest < Test::Unit::TestCase
get :index, :user_id => users(:first) get :index, :user_id => users(:first)
end end
should_respond_with :success 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 should_not_assign_to :foo, :bar
end end