Skip to content

Commit

Permalink
add a :class option to should_assign_to which checks that the instanc…
Browse files Browse the repository at this point in the history
…e vars are of the correct class
  • Loading branch information
mjankowski authored and Tammer Saleh committed Sep 20, 2008
1 parent 2c41a56 commit a62c376
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/shoulda/controller/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a62c376

Please sign in to comment.