Skip to content
Browse files

Deprecated the implicit instance variable in favor of the explicit su…

…bject method [#86 state:resolved]
  • Loading branch information...
1 parent 7e17dec commit cbafde841beab3dbcf89b034fc41cc922054573e @jferris jferris committed May 7, 2009
Showing with 19 additions and 12 deletions.
  1. +4 −4 lib/shoulda/active_record/assertions.rb
  2. +13 −2 lib/shoulda/context.rb
  3. +2 −6 test/unit/product_test.rb
View
8 lib/shoulda/active_record/assertions.rb
@@ -30,8 +30,8 @@ def assert_valid(obj)
#
# assert_good_value(User, :email, "user@example.com")
#
- # @product = Product.new(:tangible => false)
- # assert_good_value(Product, :price, "0")
+ # product = Product.new(:tangible => false)
+ # assert_good_value(product, :price, "0")
def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil)
object = get_instance_of(object_or_klass)
matcher = allow_value(value).
@@ -54,8 +54,8 @@ def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid
#
# assert_bad_value(User, :email, "invalid")
#
- # @product = Product.new(:tangible => true)
- # assert_bad_value(Product, :price, "0")
+ # product = Product.new(:tangible => true)
+ # assert_bad_value(product, :price, "0")
def assert_bad_value(object_or_klass, attribute, value,
error_message_to_expect = nil)
object = get_instance_of(object_or_klass)
View
15 lib/shoulda/context.rb
@@ -215,7 +215,10 @@ module InstanceMethods
# end
#
# If an instance variable exists named after the described class, that
- # instance variable will be used as the subject.
+ # instance variable will be used as the subject. This behavior is
+ # deprecated, and will be removed in a future version of Shoulda. The
+ # recommended approach for using a different subject is to use the subject
+ # class method.
#
# class UserTest
# should "be the existing user" do
@@ -241,7 +244,15 @@ def subject_block # :nodoc:
def get_instance_of(object_or_klass) # :nodoc:
if object_or_klass.is_a?(Class)
klass = object_or_klass
- instance_variable_get("@#{instance_variable_name_for(klass)}") || klass.new
+ ivar = "@#{instance_variable_name_for(klass)}"
+ if instance = instance_variable_get(ivar)
+ warn "[WARNING] Using #{ivar} as the subject. Future versions " <<
+ "of Shoulda will require an explicit subject using the " <<
+ "subject class method."
+ instance
+ else
+ klass.new
+ end
else
object_or_klass
end
View
8 test/unit/product_test.rb
@@ -2,9 +2,7 @@
class ProductTest < ActiveSupport::TestCase
context "An intangible product" do
- setup do
- @product = Product.new(:tangible => false)
- end
+ subject { Product.new(:tangible => false) }
should_validate_presence_of :title
should_not_allow_values_for :size, "22"
@@ -13,9 +11,7 @@ class ProductTest < ActiveSupport::TestCase
end
context "A tangible product" do
- setup do
- @product = Product.new(:tangible => true)
- end
+ subject { Product.new(:tangible => true) }
should_validate_presence_of :price
should_ensure_value_in_range :price, 1..9999

0 comments on commit cbafde8

Please sign in to comment.
Something went wrong with that request. Please try again.