Browse files

Added the subject method [#86]

  • Loading branch information...
1 parent e7a0650 commit 9585719c09bb895bb3c81964683a89ef92597ca9 @jferris jferris committed May 6, 2009
Showing with 54 additions and 16 deletions.
  1. +0 −13 lib/shoulda/active_record/helpers.rb
  2. +37 −0 lib/shoulda/context.rb
  3. +1 −0 lib/shoulda/test_unit.rb
  4. +16 −3 test/other/context_test.rb
View
13 lib/shoulda/active_record/helpers.rb
@@ -8,19 +8,6 @@ def pretty_error_messages(obj) # :nodoc:
end
end
- def get_instance_of(object_or_klass)
- if object_or_klass.is_a?(Class)
- klass = object_or_klass
- instance_variable_get("@#{instance_variable_name_for(klass)}") || klass.new
- else
- object_or_klass
- end
- end
-
- def instance_variable_name_for(klass)
- klass.to_s.split('::').last.underscore
- end
-
# Helper method that determines the default error message used by Active
# Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
# introduced I18n module used for localization.
View
37 lib/shoulda/context.rb
@@ -180,6 +180,43 @@ def described_type
end
end
+ module InstanceMethods
+ # Returns an instance of the class under test.
+ #
+ # class UserTest
+ # should "be a user" do
+ # assert_kind_of User, subject # passes
+ # end
+ # end
+ #
+ # If an instance variable exists named after the described class, that
+ # instance variable will be used as the subject.
+ #
+ # class UserTest
+ # should "be the existing user" do
+ # @user = User.new
+ # assert_equal @user, subject # passes
+ # end
+ # end
+ def subject
+ get_instance_of(self.class.described_type)
+ end
+
+ 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
+ else
+ object_or_klass
+ end
+ end
+
+ def instance_variable_name_for(klass) # :nodoc:
+ klass.to_s.split('::').last.underscore
+ end
+
+ end
+
class Context # :nodoc:
attr_accessor :name # my name
View
1 lib/shoulda/test_unit.rb
@@ -9,6 +9,7 @@
module Test # :nodoc: all
module Unit
class TestCase
+ include Shoulda::InstanceMethods
extend Shoulda::ClassMethods
include Shoulda::Assertions
extend Shoulda::Macros
View
19 test/other/context_test.rb
@@ -144,10 +144,23 @@ def hello; "hi"; end
end
class ::SomeModel; end
- context "described_type" do
- should "sniff the class constant from the test class" do
- self.class.expects(:name).returns("SomeModelTest")
+
+ context "given a test named after a class" do
+ setup do
+ self.class.stubs(:name).returns("SomeModelTest")
+ end
+
+ should "determine the described type" do
assert_equal SomeModel, self.class.described_type
end
+
+ should "return a new instance of the described type as the subject if none exists" do
+ assert_kind_of SomeModel, subject
+ end
+
+ should "return an existing instance of the described type as the subject" do
+ @some_model = SomeModel.new
+ assert_equal @some_model, subject
+ end
end
end

0 comments on commit 9585719

Please sign in to comment.