Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add an :equals option to should_assign_to which checks that the assig…

…ned instance variable is equal to the eval of the string passed to the option
  • Loading branch information...
commit 967b9fc67c8fbe5f96ecfb710b08993fed0f66da 1 parent b6069f7
@mjankowski mjankowski authored Tammer Saleh committed
View
18 lib/shoulda/controller/macros.rb
@@ -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
View
5 test/functional/posts_controller_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.