Permalink
Browse files

Alias `Object#method` to `Object#_method`

This will fix the problem when using Mocha to stub an object that reimplement `#method` method.
  • Loading branch information...
1 parent 5f40c6a commit 8b5053c42cc6cbb31f2e406b615ce0cf1018a836 @sikachu committed Apr 23, 2012
Showing with 16 additions and 1 deletion.
  1. +1 −1 lib/mocha/class_method.rb
  2. +3 −0 lib/mocha/object.rb
  3. +7 −0 test/unit/class_method_test.rb
  4. +5 −0 test/unit/object_test.rb
@@ -36,7 +36,7 @@ def reset_mocha
def hide_original_method
if method_exists?(method)
begin
- @original_method = stubbee.method(method)
+ @original_method = stubbee._method(method)
if @original_method && @original_method.owner == stubbee.__metaclass__
@original_visibility = :public
if stubbee.__metaclass__.protected_instance_methods.include?(method)
View
@@ -12,6 +12,9 @@ module Mocha
# Both {#expects} and {#stubs} return an {Expectation} which can be further modified by methods on {Expectation}.
module ObjectMethods
+ # @private
+ alias_method :_method, :method
+
# @private
def mocha
@mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
@@ -24,6 +24,13 @@ def test_should_not_raise_error_hiding_method_that_isnt_defined
assert_nothing_raised { method.hide_original_method }
end
+ def test_should_not_raise_error_hiding_method_in_class_that_implement_method_method
+ klass = Class.new { def self.method; end }
+ method = ClassMethod.new(klass, :method)
+
+ assert_nothing_raised { method.hide_original_method }
+ end
+
def test_should_define_a_new_method_which_should_call_mocha_method_missing
klass = Class.new { def self.method_x; end }
mocha = build_mock
View
@@ -84,4 +84,9 @@ def test_nobody_expects_the_spanish_inquisition
assert_raise(Mocha::ExpectationError) { object.expects(:the_spanish_inquisition) }
end
+ def test_should_alias_object_method
+ klass = Class.new { def self.method_x; end }
+ assert_equal klass._method(:method_x), klass.method(:method_x)
+ end
+
end

0 comments on commit 8b5053c

Please sign in to comment.