Permalink
Browse files

Fixed Issue #85

  • Loading branch information...
1 parent 1eee5ee commit 74c5df89fd3fd175d89599f3c289ae7afd682ec5 @kaiwren kaiwren committed Oct 15, 2011
Showing with 43 additions and 30 deletions.
  1. +8 −6 lib/rspec/mocks/any_instance/recorder.rb
  2. +35 −24 spec/rspec/mocks/any_instance_spec.rb
@@ -93,7 +93,7 @@ def received_expected_message!(method_name)
end
def restore_method!(method_name)
- if @klass.method_defined?(build_alias_method_name(method_name))
+ if public_protected_or_private_method_defined?(build_alias_method_name(method_name))
restore_original_method!(method_name)
else
remove_dummy_method!(method_name)
@@ -122,12 +122,14 @@ def remove_dummy_method!(method_name)
def backup_method!(method_name)
alias_method_name = build_alias_method_name(method_name)
@klass.class_eval do
- if method_defined?(method_name)
- alias_method alias_method_name, method_name
- end
- end
+ alias_method alias_method_name, method_name
+ end if public_protected_or_private_method_defined?(method_name)
end
-
+
+ def public_protected_or_private_method_defined?(method_name)
+ @klass.method_defined?(method_name) || @klass.private_method_defined?(method_name)
+ end
+
def stop_observing!(method_name)
restore_method!(method_name)
@observed_methods.delete(method_name)
@@ -644,43 +644,54 @@ class RSpec::SampleRspecTestClass;end
end
context "with stubbing" do
- before(:each) do
- klass.any_instance.stub(:existing_method).and_return(1)
- klass.method_defined?(:__existing_method_without_any_instance__).should be_true
- end
+ context "public methods" do
+ before(:each) do
+ klass.any_instance.stub(:existing_method).and_return(1)
+ klass.method_defined?(:__existing_method_without_any_instance__).should be_true
+ end
- it "restores the class to its original state after each example when no instance is created" do
- space.verify_all
+ it "restores the class to its original state after each example when no instance is created" do
+ space.verify_all
- klass.method_defined?(:__existing_method_without_any_instance__).should be_false
- klass.new.existing_method.should eq(existing_method_return_value)
- end
+ klass.method_defined?(:__existing_method_without_any_instance__).should be_false
+ klass.new.existing_method.should eq(existing_method_return_value)
+ end
- it "restores the class to its original state after each example when one instance is created" do
- klass.new.existing_method
+ it "restores the class to its original state after each example when one instance is created" do
+ klass.new.existing_method
- space.verify_all
+ space.verify_all
- klass.method_defined?(:__existing_method_without_any_instance__).should be_false
- klass.new.existing_method.should eq(existing_method_return_value)
- end
+ klass.method_defined?(:__existing_method_without_any_instance__).should be_false
+ klass.new.existing_method.should eq(existing_method_return_value)
+ end
- it "restores the class to its original state after each example when more than one instance is created" do
- klass.new.existing_method
- klass.new.existing_method
+ it "restores the class to its original state after each example when more than one instance is created" do
+ klass.new.existing_method
+ klass.new.existing_method
- space.verify_all
+ space.verify_all
- klass.method_defined?(:__existing_method_without_any_instance__).should be_false
- klass.new.existing_method.should eq(existing_method_return_value)
+ klass.method_defined?(:__existing_method_without_any_instance__).should be_false
+ klass.new.existing_method.should eq(existing_method_return_value)
+ end
end
context "private methods" do
- it "restores a stubbed private method after the spec is run (issue #85)" do
+ before :each do
klass.any_instance.stub(:private_method).and_return(:something)
-
space.verify_all
-
+ end
+
+ it "cleans up the backed up method" do
+ klass.method_defined?(:__existing_method_without_any_instance__).should be_false
+ end
+
+ it "restores a stubbed private method after the spec is run" do
+ klass.private_method_defined?(:private_method).should be_true
+ end
+
+ it "ensures that the restored method behaves as it originally did" do
klass.new.send(:private_method).should eq(:private_method_return_value)
end
end

0 comments on commit 74c5df8

Please sign in to comment.