module Shoulda::InstanceMethods
def get_instance_of(object_or_klass) # :nodoc:
def get_instance_of(object_or_klass) # :nodoc: if object_or_klass.is_a?(Class) klass = object_or_klass 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. Add this after your setup to avoid " << "this warning: subject { #{ivar} }" instance else klass.new end else object_or_klass end end
def instance_variable_name_for(klass) # :nodoc:
def instance_variable_name_for(klass) # :nodoc: klass.to_s.split('::').last.underscore end
def subject
The subject is used by all macros that require an instance of the class
end
end
assert_equal @user, subject # passes
@user = User.new
should "be the existing user" do
class UserTest
class method.
recommended approach for using a different subject is to use the subject
deprecated, and will be removed in a future version of Shoulda. The
instance variable will be used as the subject. This behavior is
If an instance variable exists named after the described class, that
end
end
assert !subject.new_record? # uses the first user
should "be an existing user" do
subject { User.first }
class UserTest
The subject can be explicitly set using the subject class method:
end
end
assert_kind_of User, subject # passes
should "be a user" do
class UserTest
Returns an instance of the class under test.
def subject if subject_block instance_eval(&subject_block) else get_instance_of(self.class.described_type) end end
def subject_block # :nodoc:
def subject_block # :nodoc: (@shoulda_context && @shoulda_context.subject_block) || self.class.subject_block end