Permalink
Showing with 191 additions and 3 deletions.
  1. +9 −3 lib/mocha/central.rb
  2. +1 −0 lib/mocha/mock.rb
  3. +33 −0 lib/mocha/object.rb
  4. +122 −0 test/acceptance/unstubbing_test.rb
  5. +26 −0 test/unit/central_test.rb
@@ -15,10 +15,16 @@ def stub(method)
end
end

def unstub_all
while stubba_methods.length > 0
method = stubba_methods.pop
def unstub(method)
if stubba_methods.include?(method)
method.unstub
stubba_methods.delete(method)
end
end

def unstub_all
while stubba_methods.any? do
unstub(stubba_methods.first)
end
end

@@ -6,6 +6,7 @@
require 'mocha/parameters_matcher'
require 'mocha/unexpected_invocation'
require 'mocha/argument_iterator'
require 'mocha/mockery'

module Mocha # :nodoc:

@@ -101,6 +101,39 @@ def stubs(method_name_or_hash)
}
expectation
end

# :call-seq: unstub(*method_names)
#
# Removes the method stub added by calls to #expects or #stubs.
# Restores the original behaviour of the method before it was stubbed.
# multiplier = Multiplier.new
# multiplier.double(2) # => 4
# multiplier.stubs(:double).raises
# multiplier.double(2) # => raises exception
# multiplier.unstubs(:double)
# multiplier.double(2) # => 4
#
# The original implementation of <tt>Multiplier#double</tt> is replaced temporarily.
#
# The original implementation of <tt>Multiplier#double</tt> is restored when #unstub is called.
#
# WARNING: If you #unstub a method which still has unsatisfied expectations, you may be removing
# the only way those expectations can be satisfied. Use #unstub with care.
#
# If multiple +method_names+ are supplied, each method is unstubbed.
# multiplier.unstub(:double, :triple)
#
# # exactly equivalent to
#
# multiplier.unstub(:double)
# multiplier.unstub(:triple)
def unstub(*method_names)
mockery = Mocha::Mockery.instance
method_names.each do |method_name|
method = stubba_method.new(stubba_object, method_name)
mockery.stubba.unstub(method)
end
end

def method_exists?(method, include_public_methods = true) # :nodoc:
if include_public_methods
@@ -0,0 +1,122 @@
require File.expand_path('../acceptance_test_helper', __FILE__)
require 'mocha'

class UnstubbingTest < Test::Unit::TestCase

include AcceptanceTest

def setup
setup_acceptance_test
end

def teardown
teardown_acceptance_test
end

def test_unstubbing_an_instance_method_should_restore_original_behaviour
klass = Class.new do
def my_instance_method; :original_return_value; end
end
test_result = run_as_test do
object = klass.new
object.stubs(:my_instance_method).returns(:new_return_value)
object.unstub(:my_instance_method)
assert_equal :original_return_value, object.my_instance_method
end
assert_passed(test_result)
end

def test_unstubbing_a_class_method_should_restore_original_behaviour
klass = Class.new do
def self.my_class_method; :original_return_value; end
end
test_result = run_as_test do
klass.stubs(:my_class_method).returns(:new_return_value)
klass.unstub(:my_class_method)
assert_equal :original_return_value, klass.my_class_method
end
assert_passed(test_result)
end

def test_unstubbing_a_module_method_should_restore_original_behaviour
mod = Module.new do
def self.my_module_method; :original_return_value; end
end
test_result = run_as_test do
mod.stubs(:my_module_method).returns(:new_return_value)
mod.unstub(:my_module_method)
assert_equal :original_return_value, mod.my_module_method
end
assert_passed(test_result)
end

def test_unstubbing_an_any_instance_method_should_restore_original_behaviour
klass = Class.new do
def my_instance_method; :original_return_value; end
end
test_result = run_as_test do
object = klass.new
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
klass.any_instance.unstub(:my_instance_method)
assert_equal :original_return_value, object.my_instance_method
end
assert_passed(test_result)
end

def test_unstubbing_multiple_methods_should_restore_original_behaviour
klass = Class.new do
def my_first_instance_method; :original_return_value; end
def my_second_instance_method; :original_return_value; end
end
test_result = run_as_test do
object = klass.new
object.stubs(:my_first_instance_method).returns(:new_return_value)
object.stubs(:my_second_instance_method).returns(:new_return_value)
object.unstub(:my_first_instance_method, :my_second_instance_method)
assert_equal :original_return_value, object.my_first_instance_method
assert_equal :original_return_value, object.my_second_instance_method
end
assert_passed(test_result)
end

def test_unstubbing_a_method_multiple_times_should_restore_original_behaviour
klass = Class.new do
def my_instance_method; :original_return_value; end
end
test_result = run_as_test do
object = klass.new
object.stubs(:my_instance_method).returns(:new_return_value)
object.unstub(:my_instance_method)
object.unstub(:my_instance_method)
assert_equal :original_return_value, object.my_instance_method
end
assert_passed(test_result)
end

def test_unstubbing_a_non_stubbed_method_should_do_nothing
klass = Class.new do
def my_instance_method; :original_return_value; end
end
test_result = run_as_test do
object = klass.new
object.unstub(:my_instance_method)
assert_equal :original_return_value, object.my_instance_method
end
assert_passed(test_result)
end

def test_unstubbing_a_method_which_was_stubbed_multiple_times_should_restore_orginal_behaviour
klass = Class.new do
def my_instance_method; :original_return_value; end
end
test_result = run_as_test do
object = klass.new
object.stubs(:my_instance_method).with(:first).returns(:first_new_return_value)
object.stubs(:my_instance_method).with(:second).returns(:second_new_return_value)
object.unstub(:my_instance_method)
assert_equal :original_return_value, object.my_instance_method
end
assert_passed(test_result)
end

end
@@ -47,6 +47,32 @@ def test_should_record_method
assert_equal [method], stubba.stubba_methods
end

def test_should_unstub_specified_method
stubba = Central.new
method_1 = Mock.new
method_2 = Mock.new
method_2.expects(:unstub)
stubba.stubba_methods = [method_1, method_2]

stubba.unstub(method_2)

assert_equal [method_1], stubba.stubba_methods
assert method_2.__verified__?
end

def test_should_not_unstub_specified_method_if_not_already_stubbed
stubba = Central.new
method_1 = Mock.new
method_2 = Mock.new
method_2.expects(:unstub).never
stubba.stubba_methods = [method_1]

stubba.unstub(method_2)

assert_equal [method_1], stubba.stubba_methods
assert method_2.__verified__?
end

def test_should_unstub_all_methods
stubba = Central.new
method_1 = Mock.new

0 comments on commit 187eddf

Please sign in to comment.