Skip to content

Commit

Permalink
Fixed bug with restoring original private class method
Browse files Browse the repository at this point in the history
  • Loading branch information
David Genord II committed Apr 28, 2010
1 parent c987294 commit bdb3eb3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/rspec/mocks/method_double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def stash_original_method
stashed = stashed_method_name
orig = @method_name
object_singleton_class.class_eval do
alias_method(stashed, orig) if method_defined?(orig)
alias_method(stashed, orig) if method_defined?(orig) || private_method_defined?(orig)
end
@stashed = true
end
Expand All @@ -91,7 +91,7 @@ def restore_original_method
stashed_method_name = self.stashed_method_name
object_singleton_class.instance_eval do
remove_method method_name
if method_defined?(stashed_method_name)
if method_defined?(stashed_method_name) || private_method_defined?(stashed_method_name)
alias_method method_name, stashed_method_name
remove_method stashed_method_name
end
Expand Down
34 changes: 31 additions & 3 deletions spec/rspec/mocks/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ module Mocks
describe "A method stub" do
before(:each) do
@class = Class.new do
def self.existing_class_method
:original_value
class << self
def existing_class_method
existing_private_class_method
end

private
def existing_private_class_method
:original_value
end
end

def existing_instance_method
existing_private_instance_method
end

private
def existing_private_instance_method
:original_value
end
end
Expand Down Expand Up @@ -90,7 +102,15 @@ def existing_instance_method
@instance.rspec_verify
@instance.existing_instance_method.should equal(:original_value)
end


it "should revert to original private instance method if there is one" do
@instance.existing_instance_method.should equal(:original_value)
@instance.stub(:existing_private_instance_method).and_return(:mock_value)
@instance.existing_instance_method.should equal(:mock_value)
@instance.rspec_verify
@instance.existing_instance_method.should equal(:original_value)
end

it "should revert to original class method if there is one" do
@class.existing_class_method.should equal(:original_value)
@class.stub(:existing_class_method).and_return(:mock_value)
Expand All @@ -99,6 +119,14 @@ def existing_instance_method
@class.existing_class_method.should equal(:original_value)
end

it "should revert to original private class method if there is one" do
@class.existing_class_method.should equal(:original_value)
@class.stub(:existing_private_class_method).and_return(:mock_value)
@class.existing_class_method.should equal(:mock_value)
@class.rspec_verify
@class.existing_class_method.should equal(:original_value)
end

it "should yield a specified object" do
@instance.stub(:method_that_yields).and_yield(:yielded_obj)
current_value = :value_before
Expand Down

0 comments on commit bdb3eb3

Please sign in to comment.