Skip to content

Commit

Permalink
Safer restoration of stubbed method visibility. Fixes #141.
Browse files Browse the repository at this point in the history
It is possible for `Module#public`, `#protected` and `#private` to be
redefined on descendant classes, so it' safer to use the original
version of these methods on `Module` when restoring the visibility of
stubbed methods.

Rather than add a completely new set of tests for these scenarios, I
have just embellished the existing tests. It's not ideal, because it
makes it less clear what's going on, but I think it'll do.
  • Loading branch information
tmm1 authored and floehopper committed Mar 6, 2013
1 parent 3f56bd9 commit 2b3148c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/mocha/any_instance_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def remove_new_method
def restore_original_method
if @original_method && @original_method.owner == stubbee
stubbee.send(:define_method, method, @original_method)
stubbee.send(@original_visibility, method)
Module.instance_method(@original_visibility).bind(stubbee).call(method)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/class_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def restore_original_method
end
end
if @original_visibility
stubbee.__metaclass__.send(@original_visibility, method)
Module.instance_method(@original_visibility).bind(stubbee.__metaclass__).call(method)
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/acceptance/stub_any_instance_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def test_should_leave_stubbed_public_method_unchanged_after_test
def my_instance_method
:original_return_value
end
public :my_instance_method
def self.public(*args); end
end
instance = klass.new
run_as_test do
Expand All @@ -47,6 +49,7 @@ def my_instance_method
:original_return_value
end
protected :my_instance_method
def self.protected(*args); end
def my_unprotected_instance_method
my_instance_method
end
Expand All @@ -65,6 +68,7 @@ def my_instance_method
:original_return_value
end
private :my_instance_method
def self.private(*args); end
end
instance = klass.new
run_as_test do
Expand Down
3 changes: 3 additions & 0 deletions test/acceptance/stub_class_method_defined_on_class_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def my_class_method
:original_return_value
end
public :my_class_method
def self.public(*args); end
end
end
assert_snapshot_unchanged(klass) do
Expand All @@ -39,6 +40,7 @@ def my_class_method
:original_return_value
end
protected :my_class_method
def self.protected(*args); end
end
end
assert_snapshot_unchanged(klass) do
Expand All @@ -58,6 +60,7 @@ def my_class_method
:original_return_value
end
private :my_class_method
def self.private(*args); end
end
end
assert_snapshot_unchanged(klass) do
Expand Down

0 comments on commit 2b3148c

Please sign in to comment.