Skip to content

Commit

Permalink
add an :equals option to should_assign_to which checks that the assig…
Browse files Browse the repository at this point in the history
…ned instance variable is equal to the eval of the string passed to the option
  • Loading branch information
mjankowski authored and Tammer Saleh committed Sep 20, 2008
1 parent b6069f7 commit 967b9fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
18 changes: 15 additions & 3 deletions lib/shoulda/controller/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,31 @@ def should_not_set_the_flash
#
# Options:
# * <tt>:class</tt> - The expected class of the instance variable being checked.
# * <tt>:equals</tt> - A string which is evaluated and compared for equality with
# the instance variable being checked.
#
# Example:
#
# should_assign_to :user, :posts
# should_assign_to :user, :class => User
# should_assign_to :user, :equals => '@user'
def should_assign_to(*names)
opts = names.extract_options!
names.each do |name|
test_name = "assign @#{name}"
test_name << " as #{opts[:class]}" if opts[:class]
test_name << " as class #{opts[:class]}" if opts[:class]
test_name << " which is equal to #{opts[:equals]}" if opts[:equals]
should test_name do
assert assigns(name.to_sym), "The action isn't assigning to @#{name}"
assert_kind_of opts[:class], assigns(name.to_sym) if opts[:class]
assigned_value = assigns(name.to_sym)
assert assigned_value, "The action isn't assigning to @#{name}"
assert_kind_of opts[:class], assigned_value if opts[:class]
if opts[:equals]
instantiate_variables_from_assigns do
expected_value = eval(opts[:equals], self.send(:binding), __FILE__, __LINE__)
assert_equal expected_value, assigned_value,
"Instance variable @#{name} expected to be #{expected_value} but was #{assigned_value}"
end
end
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ def setup
get :index, :user_id => users(:first)
end
should_respond_with :success
should_assign_to :user, :class => User
should_assign_to :user, :class => User, :equals => 'users(:first)'
should_fail do
should_assign_to :user, :class => Post
end
should_fail do
should_assign_to :user, :equals => 'posts(:first)'
end
should_assign_to :posts
should_not_assign_to :foo, :bar
end
Expand Down

0 comments on commit 967b9fc

Please sign in to comment.