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:
parent
2c41a563f9
commit
a62c376628
2 changed files with 13 additions and 1 deletions
|
@ -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:
|
||||
# * <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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue