mocha-macruby 0.9.8.20100129120100 → 0.9.9.20101102121900

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/COPYING.rdoc +3 -0
  2. data/{MIT-LICENSE → MIT-LICENSE.rdoc} +0 -0
  3. data/{README → README.rdoc} +7 -7
  4. data/{RELEASE → RELEASE.rdoc} +18 -0
  5. data/Rakefile +44 -28
  6. data/lib/mocha.rb +1 -0
  7. data/lib/mocha/class_method.rb +1 -3
  8. data/lib/mocha/expectation.rb +37 -34
  9. data/lib/mocha/integration/mini_test.rb +32 -4
  10. data/lib/mocha/integration/mini_test/exception_translation.rb +20 -0
  11. data/lib/mocha/integration/mini_test/{version_131_and_above.rb → version_13.rb} +7 -13
  12. data/lib/mocha/integration/mini_test/version_140.rb +52 -0
  13. data/lib/mocha/integration/mini_test/version_141.rb +56 -0
  14. data/lib/mocha/integration/mini_test/version_142_and_above.rb +47 -0
  15. data/lib/mocha/integration/test_unit.rb +23 -8
  16. data/lib/mocha/integration/test_unit/gem_version_200.rb +3 -0
  17. data/lib/mocha/integration/test_unit/{gem_version_201_and_above.rb → gem_version_201_to_202.rb} +4 -1
  18. data/lib/mocha/integration/test_unit/gem_version_203_to_209.rb +52 -0
  19. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +3 -0
  20. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +3 -0
  21. data/lib/mocha/mock.rb +33 -18
  22. data/lib/mocha/module_method.rb +1 -1
  23. data/lib/mocha/object.rb +144 -160
  24. data/lib/mocha/options.rb +1 -0
  25. data/lib/mocha/parameter_matchers/base.rb +50 -2
  26. data/test/acceptance/acceptance_test_helper.rb +1 -1
  27. data/test/acceptance/api_test.rb +1 -1
  28. data/test/acceptance/bug_18914_test.rb +1 -1
  29. data/test/acceptance/bug_21465_test.rb +1 -1
  30. data/test/acceptance/bug_21563_test.rb +1 -1
  31. data/test/acceptance/expected_invocation_count_test.rb +1 -1
  32. data/test/acceptance/failure_messages_test.rb +1 -1
  33. data/test/acceptance/minitest_test.rb +128 -124
  34. data/test/acceptance/mocha_example_test.rb +1 -1
  35. data/test/acceptance/mocha_test_result_test.rb +1 -1
  36. data/test/acceptance/mock_test.rb +1 -1
  37. data/test/acceptance/mock_with_initializer_block_test.rb +1 -1
  38. data/test/acceptance/mocked_methods_dispatch_test.rb +1 -1
  39. data/test/acceptance/optional_parameters_test.rb +1 -1
  40. data/test/acceptance/parameter_matcher_test.rb +38 -1
  41. data/test/acceptance/partial_mocks_test.rb +1 -1
  42. data/test/acceptance/return_value_test.rb +1 -1
  43. data/test/acceptance/sequence_test.rb +1 -1
  44. data/test/acceptance/states_test.rb +1 -1
  45. data/test/acceptance/stub_any_instance_method_test.rb +5 -2
  46. data/test/acceptance/stub_class_method_test.rb +5 -2
  47. data/test/acceptance/stub_everything_test.rb +1 -1
  48. data/test/acceptance/stub_instance_method_test.rb +5 -2
  49. data/test/acceptance/stub_module_method_test.rb +3 -3
  50. data/test/acceptance/stub_test.rb +1 -1
  51. data/test/acceptance/stubba_example_test.rb +1 -1
  52. data/test/acceptance/stubba_test.rb +1 -1
  53. data/test/acceptance/stubba_test_result_test.rb +1 -1
  54. data/test/acceptance/stubbing_error_backtrace_test.rb +1 -1
  55. data/test/acceptance/stubbing_method_unnecessarily_test.rb +1 -1
  56. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +5 -5
  57. data/test/acceptance/stubbing_non_existent_class_method_test.rb +5 -5
  58. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +5 -5
  59. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +9 -9
  60. data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
  61. data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
  62. data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
  63. data/test/mini_test_result.rb +74 -0
  64. data/test/test_helper.rb +3 -1
  65. data/test/test_runner.rb +25 -11
  66. data/test/unit/any_instance_method_test.rb +1 -1
  67. data/test/unit/array_inspect_test.rb +1 -1
  68. data/test/unit/backtrace_filter_test.rb +1 -1
  69. data/test/unit/cardinality_test.rb +1 -1
  70. data/test/unit/central_test.rb +1 -1
  71. data/test/unit/change_state_side_effect_test.rb +1 -1
  72. data/test/unit/class_method_test.rb +2 -2
  73. data/test/unit/configuration_test.rb +1 -1
  74. data/test/unit/date_time_inspect_test.rb +1 -1
  75. data/test/unit/exception_raiser_test.rb +1 -1
  76. data/test/unit/expectation_list_test.rb +1 -1
  77. data/test/unit/expectation_test.rb +1 -1
  78. data/test/unit/hash_inspect_test.rb +1 -1
  79. data/test/unit/in_state_ordering_constraint_test.rb +1 -1
  80. data/test/unit/metaclass_test.rb +1 -1
  81. data/test/unit/method_matcher_test.rb +1 -1
  82. data/test/unit/mock_test.rb +16 -14
  83. data/test/unit/mockery_test.rb +1 -1
  84. data/test/unit/multiple_yields_test.rb +1 -1
  85. data/test/unit/no_yields_test.rb +1 -1
  86. data/test/unit/object_inspect_test.rb +10 -9
  87. data/test/unit/object_test.rb +11 -10
  88. data/test/unit/parameter_matchers/all_of_test.rb +1 -1
  89. data/test/unit/parameter_matchers/any_of_test.rb +1 -1
  90. data/test/unit/parameter_matchers/anything_test.rb +1 -1
  91. data/test/unit/parameter_matchers/equals_test.rb +1 -1
  92. data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
  93. data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
  94. data/test/unit/parameter_matchers/has_key_test.rb +1 -1
  95. data/test/unit/parameter_matchers/has_value_test.rb +1 -1
  96. data/test/unit/parameter_matchers/includes_test.rb +5 -5
  97. data/test/unit/parameter_matchers/instance_of_test.rb +2 -2
  98. data/test/unit/parameter_matchers/is_a_test.rb +1 -1
  99. data/test/unit/parameter_matchers/kind_of_test.rb +1 -1
  100. data/test/unit/parameter_matchers/not_test.rb +1 -1
  101. data/test/unit/parameter_matchers/regexp_matches_test.rb +1 -1
  102. data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
  103. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +1 -1
  104. data/test/unit/parameters_matcher_test.rb +1 -1
  105. data/test/unit/return_values_test.rb +1 -1
  106. data/test/unit/sequence_test.rb +1 -1
  107. data/test/unit/single_return_value_test.rb +1 -1
  108. data/test/unit/single_yield_test.rb +1 -1
  109. data/test/unit/state_machine_test.rb +1 -1
  110. data/test/unit/string_inspect_test.rb +1 -1
  111. data/test/unit/yield_parameters_test.rb +1 -1
  112. metadata +43 -19
  113. data/COPYING +0 -3
@@ -2,7 +2,11 @@ require 'mocha/api'
2
2
 
3
3
  if !MiniTest::Unit::TestCase.ancestors.include?(Mocha::API)
4
4
 
5
- require 'mocha/integration/mini_test/version_131_and_above'
5
+ require 'mocha/integration/mini_test/exception_translation'
6
+ require 'mocha/integration/mini_test/version_13'
7
+ require 'mocha/integration/mini_test/version_140'
8
+ require 'mocha/integration/mini_test/version_141'
9
+ require 'mocha/integration/mini_test/version_142_and_above'
6
10
 
7
11
  module MiniTest
8
12
  class Unit
@@ -10,10 +14,34 @@ if !MiniTest::Unit::TestCase.ancestors.include?(Mocha::API)
10
14
 
11
15
  include Mocha::API
12
16
 
13
- alias_method :run_before_mocha, :run
14
- remove_method :run
17
+ # For now workaround a MacRuby bug which makes it impossible to
18
+ # actually remove a method and include a module which defines it again.
19
+ #
20
+ #alias_method :run_before_mocha, :run
21
+ #remove_method :run
15
22
 
16
- include Mocha::Integration::MiniTest::Version131AndAbove
23
+ #mini_test_version = begin
24
+ #MiniTest::Unit::VERSION
25
+ #rescue LoadError
26
+ #'unknown'
27
+ #end
28
+
29
+ #$stderr.puts "Detected MiniTest version: #{mini_test_version}" if $options['debug']
30
+
31
+ #if (mini_test_version >= '1.3.0') && (mini_test_version <= '1.3.1')
32
+ #include Mocha::Integration::MiniTest::Version13
33
+ #elsif (mini_test_version == '1.4.0')
34
+ #include Mocha::Integration::MiniTest::Version140
35
+ #elsif (mini_test_version == '1.4.1')
36
+ #include Mocha::Integration::MiniTest::Version141
37
+ #elsif (mini_test_version >= '1.4.2') && (mini_test_version <= '1.6.0')
38
+ #include Mocha::Integration::MiniTest::Version142AndAbove
39
+ #elsif (mini_test_version > '1.6.0')
40
+ #$stderr.puts "*** MiniTest integration has not been verified but patching anyway ***" if $options['debug']
41
+ #include Mocha::Integration::MiniTest::Version142AndAbove
42
+ #else
43
+ #$stderr.puts "*** No Mocha integration for MiniTest version ***" if $options['debug']
44
+ #end
17
45
 
18
46
  end
19
47
  end
@@ -0,0 +1,20 @@
1
+ require 'mocha/expectation_error'
2
+
3
+ module Mocha
4
+
5
+ module Integration
6
+
7
+ module MiniTest
8
+
9
+ def self.translate(exception)
10
+ return exception unless exception.kind_of?(::Mocha::ExpectationError)
11
+ translated_exception = ::MiniTest::Assertion.new(exception.message)
12
+ translated_exception.set_backtrace(exception.backtrace)
13
+ translated_exception
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -1,5 +1,4 @@
1
1
  require 'mocha/integration/mini_test/assertion_counter'
2
- require 'mocha/expectation_error'
3
2
 
4
3
  module Mocha
5
4
 
@@ -7,33 +6,28 @@ module Mocha
7
6
 
8
7
  module MiniTest
9
8
 
10
- def self.translate(exception)
11
- return exception unless exception.kind_of?(::Mocha::ExpectationError)
12
- translated_exception = ::MiniTest::Assertion.new(exception.message)
13
- translated_exception.set_backtrace(exception.backtrace)
14
- translated_exception
15
- end
16
-
17
- module Version131AndAbove
9
+ module Version13
10
+ def self.included(mod)
11
+ $stderr.puts "Monkey patching MiniTest v1.3" if $options['debug']
12
+ end
18
13
  def run runner
19
14
  assertion_counter = AssertionCounter.new(self)
20
15
  result = '.'
21
- name = (self.respond_to?(:name) ? self.name : self.__name__)
22
16
  begin
23
17
  begin
24
18
  @passed = nil
25
19
  self.setup
26
- self.__send__ name
20
+ self.__send__ self.name
27
21
  mocha_verify(assertion_counter)
28
22
  @passed = true
29
23
  rescue Exception => e
30
24
  @passed = false
31
- result = runner.puke(self.class, name, Mocha::Integration::MiniTest.translate(e))
25
+ result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
32
26
  ensure
33
27
  begin
34
28
  self.teardown
35
29
  rescue Exception => e
36
- result = runner.puke(self.class, name, Mocha::Integration::MiniTest.translate(e))
30
+ result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
37
31
  end
38
32
  end
39
33
  ensure
@@ -0,0 +1,52 @@
1
+ require 'mocha/integration/mini_test/assertion_counter'
2
+ require 'mocha/expectation_error'
3
+
4
+ module Mocha
5
+
6
+ module Integration
7
+
8
+ module MiniTest
9
+
10
+ module Version140
11
+ def self.included(mod)
12
+ $stderr.puts "Monkey patching MiniTest v1.4.0" if $options['debug']
13
+ end
14
+ def run runner
15
+ assertion_counter = AssertionCounter.new(self)
16
+ result = '.'
17
+ begin
18
+ @passed = nil
19
+ self.setup
20
+ self.__send__ name
21
+ mocha_verify(assertion_counter)
22
+ @passed = true
23
+ rescue Exception => e
24
+ @passed = false
25
+ result = runner.puke(self.class, name, self.class.translate_exception(e))
26
+ ensure
27
+ begin
28
+ @passed = nil
29
+ self.setup
30
+ self.__send__ self.__name__
31
+ mocha_verify(assertion_counter)
32
+ @passed = true
33
+ rescue Exception => e
34
+ @passed = false
35
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
36
+ ensure
37
+ begin
38
+ self.teardown
39
+ rescue Exception => e
40
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
41
+ end
42
+ end
43
+ end
44
+ ensure
45
+ mocha_teardown
46
+ end
47
+ result
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,56 @@
1
+ require 'mocha/integration/mini_test/assertion_counter'
2
+ require 'mocha/expectation_error'
3
+
4
+ module Mocha
5
+
6
+ module Integration
7
+
8
+ module MiniTest
9
+
10
+ module Version141
11
+ def self.included(mod)
12
+ $stderr.puts "Monkey patching MiniTest v1.4.1"
13
+ end
14
+ def run runner
15
+ trap 'INFO' do
16
+ warn '%s#%s %.2fs' % [self.class, self.__name__,
17
+ (Time.now - runner.start_time)]
18
+ runner.status $stderr
19
+ end
20
+
21
+ assertion_counter = AssertionCounter.new(self)
22
+ result = '.'
23
+ begin
24
+ begin
25
+ @passed = nil
26
+ self.setup
27
+ self.__send__ self.__name__
28
+ mocha_verify(assertion_counter)
29
+ @passed = true
30
+ rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
31
+ raise
32
+ rescue Exception => e
33
+ @passed = false
34
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
35
+ ensure
36
+ begin
37
+ self.teardown
38
+ rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
39
+ raise
40
+ rescue Exception => e
41
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
42
+ end
43
+ trap 'INFO', 'DEFAULT'
44
+ end
45
+ ensure
46
+ mocha_teardown
47
+ end
48
+ result
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,47 @@
1
+ require 'mocha/integration/mini_test/assertion_counter'
2
+ require 'mocha/expectation_error'
3
+
4
+ module MiniTest
5
+ class Unit
6
+ class TestCase
7
+ $stderr.puts "Monkey patching MiniTest >= v1.4.2" if $options['debug']
8
+
9
+ def run runner
10
+ trap 'INFO' do
11
+ warn '%s#%s %.2fs' % [self.class, self.__name__,
12
+ (Time.now - runner.start_time)]
13
+ runner.status $stderr
14
+ end if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
15
+
16
+ assertion_counter = AssertionCounter.new(self)
17
+ result = '.'
18
+ begin
19
+ @passed = nil
20
+ self.setup
21
+ self.__send__ self.__name__
22
+ mocha_verify(assertion_counter)
23
+ @passed = true
24
+ rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
25
+ raise
26
+ rescue Exception => e
27
+ @passed = false
28
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
29
+ ensure
30
+ begin
31
+ self.teardown
32
+ rescue *PASSTHROUGH_EXCEPTIONS
33
+ raise
34
+ rescue Exception => e
35
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
36
+ end
37
+ trap 'INFO', 'DEFAULT' if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
38
+ end
39
+ ensure
40
+ mocha_teardown
41
+ end
42
+ result
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -3,7 +3,8 @@ require 'mocha/api'
3
3
  if !Test::Unit::TestCase.ancestors.include?(Mocha::API)
4
4
 
5
5
  require 'mocha/integration/test_unit/gem_version_200'
6
- require 'mocha/integration/test_unit/gem_version_201_and_above'
6
+ require 'mocha/integration/test_unit/gem_version_201_to_202'
7
+ require 'mocha/integration/test_unit/gem_version_203_to_209'
7
8
  require 'mocha/integration/test_unit/ruby_version_185_and_below'
8
9
  require 'mocha/integration/test_unit/ruby_version_186_and_above'
9
10
 
@@ -17,20 +18,34 @@ if !Test::Unit::TestCase.ancestors.include?(Mocha::API)
17
18
  remove_method :run
18
19
 
19
20
  test_unit_version = begin
20
- require 'test/unit/version'
21
+ load 'test/unit/version.rb'
21
22
  Test::Unit::VERSION
22
23
  rescue LoadError
23
24
  '1.x'
24
25
  end
25
26
 
26
- if test_unit_version == '2.0.0'
27
+ if $options['debug']
28
+ $stderr.puts "Detected Ruby version: #{RUBY_VERSION}"
29
+ $stderr.puts "Detected Test::Unit version: #{test_unit_version}"
30
+ end
31
+
32
+ if (test_unit_version == '1.x') || (test_unit_version == '1.2.3')
33
+ if RUBY_VERSION < '1.8.6'
34
+ include Mocha::Integration::TestUnit::RubyVersion185AndBelow
35
+ else
36
+ include Mocha::Integration::TestUnit::RubyVersion186AndAbove
37
+ end
38
+ elsif (test_unit_version == '2.0.0')
27
39
  include Mocha::Integration::TestUnit::GemVersion200
28
- elsif test_unit_version >= '2.0.1'
29
- include Mocha::Integration::TestUnit::GemVersion201AndAbove
30
- elsif RUBY_VERSION < '1.8.6'
31
- include Mocha::Integration::TestUnit::RubyVersion185AndBelow
40
+ elsif (test_unit_version >= '2.0.1') && (test_unit_version <= '2.0.2')
41
+ include Mocha::Integration::TestUnit::GemVersion201To202
42
+ elsif (test_unit_version >= '2.0.3') && (test_unit_version <= '2.0.9')
43
+ include Mocha::Integration::TestUnit::GemVersion203To209
44
+ elsif (test_unit_version > '2.0.9')
45
+ $stderr.puts "*** Test::Unit integration has not been verified but patching anyway ***" if $options['debug']
46
+ include Mocha::Integration::TestUnit::GemVersion203To209
32
47
  else
33
- include Mocha::Integration::TestUnit::RubyVersion186AndAbove
48
+ $stderr.puts "*** No Mocha integration for Test::Unit version ***" if $options['debug']
34
49
  end
35
50
 
36
51
  end
@@ -9,6 +9,9 @@ module Mocha
9
9
  module TestUnit
10
10
 
11
11
  module GemVersion200
12
+ def self.included(mod)
13
+ $stderr.puts "Monkey patching Test::Unit gem v2.0.0" if $options['debug']
14
+ end
12
15
  def run(result)
13
16
  assertion_counter = AssertionCounter.new(result)
14
17
  begin
@@ -8,7 +8,10 @@ module Mocha
8
8
 
9
9
  module TestUnit
10
10
 
11
- module GemVersion201AndAbove
11
+ module GemVersion201To202
12
+ def self.included(mod)
13
+ $stderr.puts "Monkey patching Test::Unit gem >= v2.0.1 and <= v2.0.2" if $options['debug']
14
+ end
12
15
  def run(result)
13
16
  assertion_counter = AssertionCounter.new(result)
14
17
  begin
@@ -0,0 +1,52 @@
1
+ require 'test/unit/testcase'
2
+ require 'mocha/integration/test_unit/assertion_counter'
3
+ require 'mocha/expectation_error'
4
+
5
+ module Mocha
6
+
7
+ module Integration
8
+
9
+ module TestUnit
10
+
11
+ module GemVersion203To209
12
+ def self.included(mod)
13
+ $stderr.puts "Monkey patching Test::Unit gem >= v2.0.3 and <= v2.0.9" if $options['debug']
14
+ end
15
+ def run(result)
16
+ assertion_counter = AssertionCounter.new(result)
17
+ begin
18
+ @_result = result
19
+ yield(Test::Unit::TestCase::STARTED, name)
20
+ begin
21
+ begin
22
+ run_setup
23
+ run_test
24
+ mocha_verify(assertion_counter)
25
+ rescue Mocha::ExpectationError => e
26
+ add_failure(e.message, e.backtrace)
27
+ rescue Exception
28
+ @interrupted = true
29
+ raise unless handle_exception($!)
30
+ ensure
31
+ begin
32
+ run_teardown
33
+ rescue Exception
34
+ raise unless handle_exception($!)
35
+ end
36
+ end
37
+ ensure
38
+ mocha_teardown
39
+ end
40
+ result.add_run
41
+ yield(Test::Unit::TestCase::FINISHED, name)
42
+ ensure
43
+ # @_result = nil # For test-spec's after_all :<
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -9,6 +9,9 @@ module Mocha
9
9
  module TestUnit
10
10
 
11
11
  module RubyVersion185AndBelow
12
+ def self.included(mod)
13
+ $stderr.puts "Monkey patching Test::Unit for Ruby <= v1.8.5" if $options['debug']
14
+ end
12
15
  def run(result)
13
16
  assertion_counter = AssertionCounter.new(result)
14
17
  yield(Test::Unit::TestCase::STARTED, name)
@@ -9,6 +9,9 @@ module Mocha
9
9
  module TestUnit
10
10
 
11
11
  module RubyVersion186AndAbove
12
+ def self.included(mod)
13
+ $stderr.puts "Monkey patching Test::Unit for Ruby >= v1.8.6" if $options['debug']
14
+ end
12
15
  def run(result)
13
16
  assertion_counter = AssertionCounter.new(result)
14
17
  yield(Test::Unit::TestCase::STARTED, name)
data/lib/mocha/mock.rb CHANGED
@@ -2,7 +2,6 @@ require 'mocha/expectation'
2
2
  require 'mocha/expectation_list'
3
3
  require 'mocha/metaclass'
4
4
  require 'mocha/names'
5
- require 'mocha/mockery'
6
5
  require 'mocha/method_matcher'
7
6
  require 'mocha/parameters_matcher'
8
7
  require 'mocha/unexpected_invocation'
@@ -43,7 +42,7 @@ module Mocha # :nodoc:
43
42
  iterator = ArgumentIterator.new(method_name_or_hash)
44
43
  iterator.each { |*args|
45
44
  method_name = args.shift
46
- ensure_method_not_already_defined(method_name)
45
+ __define_stub_method__(method_name)
47
46
  expectation = Expectation.new(self, method_name, backtrace)
48
47
  expectation.returns(args.shift) if args.length > 0
49
48
  @expectations.add(expectation)
@@ -75,7 +74,7 @@ module Mocha # :nodoc:
75
74
  iterator = ArgumentIterator.new(method_name_or_hash)
76
75
  iterator.each { |*args|
77
76
  method_name = args.shift
78
- ensure_method_not_already_defined(method_name)
77
+ __define_stub_method__(method_name)
79
78
  expectation = Expectation.new(self, method_name, backtrace)
80
79
  expectation.at_least(0)
81
80
  expectation.returns(args.shift) if args.length > 0
@@ -148,21 +147,11 @@ module Mocha # :nodoc:
148
147
 
149
148
  def stub_everything
150
149
  @everything_stubbed = true
150
+ ANCESTOR_INSTANCE_METHODS.each { |m| __define_stub_method__(m) }
151
151
  end
152
152
 
153
153
  def method_missing(symbol, *arguments, &block)
154
- if @responder and not @responder.respond_to?(symbol)
155
- raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
156
- end
157
- if matching_expectation_allowing_invocation = @expectations.match_allowing_invocation(symbol, *arguments)
158
- matching_expectation_allowing_invocation.invoke(&block)
159
- else
160
- if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
161
- message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
162
- message << Mockery.instance.mocha_inspect
163
- raise ExpectationError.new(message, caller)
164
- end
165
- end
154
+ __match_invocation__(symbol, arguments, block)
166
155
  end
167
156
 
168
157
  def respond_to?(symbol, include_private = false)
@@ -188,11 +177,37 @@ module Mocha # :nodoc:
188
177
  def inspect
189
178
  mocha_inspect
190
179
  end
191
-
192
- def ensure_method_not_already_defined(method_name)
193
- self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
180
+
181
+ def __define_stub_method__(symbol)
182
+ verbose, $VERBOSE = $VERBOSE, nil
183
+ __metaclass__.class_eval(%{
184
+ def #{symbol}(*args, &block)
185
+ __match_invocation__('#{symbol}'.to_sym, args, block)
186
+ end
187
+ })
188
+ ensure
189
+ $VERBOSE = verbose
194
190
  end
195
191
 
192
+ def __match_invocation__(symbol, arguments, block)
193
+ if @responder and not @responder.respond_to?(symbol)
194
+ raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
195
+ end
196
+ if matching_expectation_allowing_invocation = @expectations.match_allowing_invocation(symbol, *arguments)
197
+ matching_expectation_allowing_invocation.invoke(&block)
198
+ else
199
+ if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
200
+ message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
201
+ message << Mockery.instance.mocha_inspect
202
+ raise ExpectationError.new(message, caller)
203
+ end
204
+ end
205
+ end
206
+
207
+ methods = instance_methods - instance_methods(false)
208
+ methods.delete(:object_id) unless RUBY_VERSION < '1.9'
209
+ ANCESTOR_INSTANCE_METHODS = methods.reject { |m| m =~ /^__.*__$/ }
210
+
196
211
  # :startdoc:
197
212
 
198
213
  end