mocha 0.12.10 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/.yardopts +22 -0
  2. data/COPYING.md +3 -0
  3. data/{MIT-LICENSE.rdoc → MIT-LICENSE.md} +0 -0
  4. data/README.md +230 -0
  5. data/{RELEASE.rdoc → RELEASE.md} +61 -56
  6. data/Rakefile +45 -37
  7. data/build-matrix.rb +16 -4
  8. data/lib/mocha.rb +6 -2
  9. data/lib/mocha/api.rb +13 -16
  10. data/lib/mocha/class_method.rb +7 -3
  11. data/lib/mocha/class_methods.rb +63 -0
  12. data/lib/mocha/debug.rb +11 -0
  13. data/lib/mocha/deprecation.rb +1 -1
  14. data/lib/mocha/exception_raiser.rb +1 -1
  15. data/lib/mocha/expectation_error.rb +5 -0
  16. data/lib/mocha/expectation_error_factory.rb +19 -0
  17. data/lib/mocha/hooks.rb +43 -0
  18. data/lib/mocha/integration.rb +6 -38
  19. data/lib/mocha/integration/assertion_counter.rb +13 -0
  20. data/lib/mocha/integration/mini_test.rb +42 -60
  21. data/lib/mocha/integration/mini_test/adapter.rb +54 -0
  22. data/lib/mocha/integration/mini_test/exception_translation.rb +0 -6
  23. data/lib/mocha/integration/mini_test/nothing.rb +19 -0
  24. data/lib/mocha/integration/mini_test/version_13.rb +32 -25
  25. data/lib/mocha/integration/mini_test/version_140.rb +32 -26
  26. data/lib/mocha/integration/mini_test/version_141.rb +40 -34
  27. data/lib/mocha/integration/mini_test/version_142_to_172.rb +40 -34
  28. data/lib/mocha/integration/mini_test/version_200.rb +41 -35
  29. data/lib/mocha/integration/mini_test/version_201_to_222.rb +41 -35
  30. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +49 -43
  31. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +53 -47
  32. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +43 -37
  33. data/lib/mocha/integration/monkey_patcher.rb +18 -0
  34. data/lib/mocha/integration/test_unit.rb +38 -45
  35. data/lib/mocha/integration/test_unit/adapter.rb +51 -0
  36. data/lib/mocha/integration/test_unit/gem_version_200.rb +36 -29
  37. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +36 -29
  38. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +36 -29
  39. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +65 -0
  40. data/lib/mocha/integration/test_unit/nothing.rb +19 -0
  41. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +36 -29
  42. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +37 -30
  43. data/lib/mocha/method_matcher.rb +1 -1
  44. data/lib/mocha/module_methods.rb +14 -0
  45. data/lib/mocha/{object.rb → object_methods.rb} +0 -83
  46. data/lib/mocha/setup.rb +10 -0
  47. data/lib/mocha/standalone.rb +3 -0
  48. data/lib/mocha/version.rb +1 -1
  49. data/lib/mocha_standalone.rb +3 -1
  50. data/mocha.gemspec +16 -9
  51. data/test/acceptance/bug_18914_test.rb +1 -1
  52. data/test/acceptance/bug_21465_test.rb +1 -1
  53. data/test/acceptance/bug_21563_test.rb +1 -1
  54. data/test/acceptance/exception_rescue_test.rb +1 -1
  55. data/test/acceptance/expectations_on_multiple_methods_test.rb +1 -1
  56. data/test/acceptance/expected_invocation_count_test.rb +1 -1
  57. data/test/acceptance/failure_messages_test.rb +1 -1
  58. data/test/acceptance/issue_65_test.rb +1 -1
  59. data/test/acceptance/issue_70_test.rb +1 -1
  60. data/test/acceptance/mocha_example_test.rb +1 -1
  61. data/test/acceptance/mocha_test_result_test.rb +1 -1
  62. data/test/acceptance/mock_test.rb +1 -1
  63. data/test/acceptance/mock_with_initializer_block_test.rb +1 -1
  64. data/test/acceptance/mocked_methods_dispatch_test.rb +1 -1
  65. data/test/acceptance/multiple_expectations_failure_message_test.rb +1 -1
  66. data/test/acceptance/optional_parameters_test.rb +1 -1
  67. data/test/acceptance/parameter_matcher_test.rb +1 -1
  68. data/test/acceptance/partial_mocks_test.rb +1 -1
  69. data/test/acceptance/raise_exception_test.rb +1 -1
  70. data/test/acceptance/return_value_test.rb +1 -1
  71. data/test/acceptance/sequence_test.rb +1 -1
  72. data/test/acceptance/states_test.rb +1 -1
  73. data/test/acceptance/stub_any_instance_method_test.rb +1 -1
  74. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +1 -1
  75. data/test/acceptance/stub_class_method_defined_on_class_test.rb +1 -1
  76. data/test/acceptance/stub_class_method_defined_on_module_test.rb +1 -1
  77. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -1
  78. data/test/acceptance/stub_everything_test.rb +1 -1
  79. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +1 -1
  80. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +1 -1
  81. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +1 -1
  82. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +1 -1
  83. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +1 -1
  84. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +1 -1
  85. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
  86. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +1 -1
  87. data/test/acceptance/stub_module_method_test.rb +1 -1
  88. data/test/acceptance/stub_test.rb +1 -1
  89. data/test/acceptance/stubba_example_test.rb +1 -1
  90. data/test/acceptance/stubba_test_result_test.rb +1 -1
  91. data/test/acceptance/stubbing_error_backtrace_test.rb +1 -1
  92. data/test/acceptance/stubbing_frozen_object_test.rb +1 -1
  93. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +1 -1
  94. data/test/acceptance/stubbing_method_unnecessarily_test.rb +1 -1
  95. data/test/acceptance/stubbing_nil_test.rb +1 -1
  96. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +1 -1
  97. data/test/acceptance/stubbing_non_existent_class_method_test.rb +1 -1
  98. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +1 -1
  99. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +1 -1
  100. data/test/acceptance/stubbing_non_public_class_method_test.rb +1 -1
  101. data/test/acceptance/stubbing_non_public_instance_method_test.rb +1 -1
  102. data/test/acceptance/stubbing_on_non_mock_object_test.rb +1 -1
  103. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +3 -3
  104. data/test/acceptance/throw_test.rb +1 -1
  105. data/test/acceptance/unstubbing_test.rb +1 -1
  106. data/test/integration/mini_test_test.rb +9 -0
  107. data/test/integration/shared_tests.rb +174 -0
  108. data/test/integration/test_unit_test.rb +9 -0
  109. data/test/mini_test_result.rb +0 -1
  110. data/test/test_runner.rb +14 -14
  111. data/test/unit/class_methods_test.rb +40 -0
  112. data/test/unit/method_matcher_test.rb +5 -0
  113. data/test/unit/mock_test.rb +6 -0
  114. data/test/unit/module_methods_test.rb +19 -0
  115. data/test/unit/object_methods_test.rb +45 -0
  116. metadata +45 -74
  117. data/COPYING.rdoc +0 -3
  118. data/README.rdoc +0 -56
  119. data/examples/misc.rb +0 -43
  120. data/examples/mocha.rb +0 -25
  121. data/examples/stubba.rb +0 -64
  122. data/gemfiles/Gemfile.minitest.3.3.0 +0 -7
  123. data/gemfiles/Gemfile.minitest.4.2.0 +0 -7
  124. data/lib/mocha/integration/mini_test/assertion_counter.rb +0 -23
  125. data/lib/mocha/integration/mini_test/version_330_to_410.rb +0 -65
  126. data/lib/mocha/integration/mini_test/version_420_to_433.rb +0 -70
  127. data/lib/mocha/integration/mini_test/version_440.rb +0 -70
  128. data/lib/mocha/integration/mini_test/version_450_to_461.rb +0 -72
  129. data/lib/mocha/integration/test_unit/assertion_counter.rb +0 -23
  130. data/lib/mocha/integration/test_unit/gem_version_230_to_254.rb +0 -58
  131. data/lib/mocha/options.rb +0 -1
  132. data/test/acceptance/api_test.rb +0 -139
  133. data/test/acceptance/minitest_test.rb +0 -162
  134. data/test/unit/object_test.rb +0 -92
data/Rakefile CHANGED
@@ -2,14 +2,20 @@ require "bundler"
2
2
  Bundler::GemHelper.install_tasks
3
3
  require "bundler/setup"
4
4
 
5
- require 'yard'
6
5
  require 'rake/testtask'
7
6
 
8
7
  desc "Run all tests"
9
8
  task 'default' => ['test', 'test:performance']
10
9
 
11
- desc "Run unit & acceptance tests"
12
- task 'test' => ['test:units', 'test:acceptance']
10
+ desc "Run tests"
11
+ task 'test' do
12
+ if test_library = ENV['MOCHA_RUN_INTEGRATION_TESTS']
13
+ Rake::Task["test:integration:#{test_library}"].invoke
14
+ else
15
+ Rake::Task['test:units'].invoke
16
+ Rake::Task['test:acceptance'].invoke
17
+ end
18
+ end
13
19
 
14
20
  namespace 'test' do
15
21
 
@@ -38,6 +44,24 @@ namespace 'test' do
38
44
  t.warning = true
39
45
  end
40
46
 
47
+ namespace 'integration' do
48
+ desc "Run MiniTest integration tests (intended to be run in its own process)"
49
+ Rake::TestTask.new('minitest') do |t|
50
+ t.libs << 'test'
51
+ t.test_files = FileList['test/integration/mini_test_test.rb']
52
+ t.verbose = true
53
+ t.warning = true
54
+ end
55
+
56
+ desc "Run Test::Unit integration tests (intended to be run in its own process)"
57
+ Rake::TestTask.new('test-unit') do |t|
58
+ t.libs << 'test'
59
+ t.test_files = FileList['test/integration/test_unit_test.rb']
60
+ t.verbose = true
61
+ t.warning = true
62
+ end
63
+ end
64
+
41
65
  # require 'rcov/rcovtask'
42
66
  # Rcov::RcovTask.new('coverage') do |t|
43
67
  # t.libs << 'test'
@@ -81,43 +105,27 @@ def benchmark_test_case(klass, iterations)
81
105
  end
82
106
  end
83
107
 
84
- desc 'Remove generated documentation'
85
- task 'clobber_yardoc' do
86
- `rm -rf ./doc`
87
- end
108
+ unless ENV["MOCHA_NO_DOCS"]
109
+ require 'yard'
88
110
 
89
- desc 'Generate documentation'
90
- YARD::Rake::YardocTask.new('yardoc') do |task|
91
- task.options = ["--title", "Mocha #{Mocha::VERSION}", "--no-private"]
92
- task.files = [
93
- 'lib/mocha/api.rb',
94
- 'lib/mocha/mock.rb',
95
- 'lib/mocha/expectation.rb',
96
- 'lib/mocha/object.rb',
97
- 'lib/mocha/parameter_matchers.rb',
98
- 'lib/mocha/parameter_matchers',
99
- 'lib/mocha/state_machine.rb',
100
- 'lib/mocha/sequence.rb',
101
- 'lib/mocha/configuration.rb',
102
- 'lib/mocha/stubbing_error.rb',
103
- 'lib/mocha/unexpected_invocation.rb',
104
- '-',
105
- 'RELEASE.rdoc',
106
- 'COPYING.rdoc',
107
- 'MIT-LICENSE.rdoc',
108
- 'examples/mocha.rb',
109
- 'examples/stubba.rb',
110
- 'examples/misc.rb',
111
- ]
112
- end
111
+ desc 'Remove generated documentation'
112
+ task 'clobber_yardoc' do
113
+ `rm -rf ./doc`
114
+ end
113
115
 
114
- desc "Generate documentation"
115
- task 'generate_docs' => ['clobber_yardoc', 'yardoc']
116
+ desc 'Generate documentation'
117
+ YARD::Rake::YardocTask.new('yardoc') do |task|
118
+ task.options = ["--title", "Mocha #{Mocha::VERSION}"]
119
+ end
120
+
121
+ desc "Generate documentation"
122
+ task 'generate_docs' => ['clobber_yardoc', 'yardoc']
116
123
 
117
- desc "Publish docs to gofreerange.com/docs/mocha"
118
- task 'publish_docs' => 'generate_docs' do
119
- path = "/home/freerange/docs/mocha"
120
- system %{ssh gofreerange.com "sudo rm -fr #{path} && mkdir -p #{path}" && scp -r doc/* gofreerange.com:#{path}}
124
+ desc "Publish docs to gofreerange.com/docs/mocha"
125
+ task 'publish_docs' => 'generate_docs' do
126
+ path = "/home/freerange/docs/mocha"
127
+ system %{ssh gofreerange.com "sudo rm -fr #{path} && mkdir -p #{path}" && scp -r doc/* gofreerange.com:#{path}}
128
+ end
121
129
  end
122
130
 
123
131
  task 'release' => 'default' do
@@ -27,13 +27,14 @@ def with_rbenv(command)
27
27
  %{export PATH="$HOME/.rbenv/bin:$PATH"; eval "$(rbenv init -)"; #{command}}
28
28
  end
29
29
 
30
- def run(gemfile)
30
+ def run(gemfile, task = "test")
31
31
  ENV["BUNDLE_GEMFILE"] = gemfile
32
32
  ENV["MOCHA_OPTIONS"] = "debug"
33
+ ENV["MOCHA_NO_DOCS"] = "true"
33
34
  reset_bundle
34
35
  execute(
35
36
  with_rbenv("bundle install --gemfile=#{gemfile}"),
36
- with_rbenv("bundle exec rake test")
37
+ with_rbenv("bundle exec rake #{task}")
37
38
  )
38
39
  end
39
40
 
@@ -44,14 +45,25 @@ EXCLUDED_RUBY_193_GEMFILES = [
44
45
  "gemfiles/Gemfile.minitest.1.4.2"
45
46
  ]
46
47
 
47
- ["1.8.7-p352", "1.9.3-p125-perf"].each do |ruby_version|
48
+ RUBY_VERSIONS = ["1.8.7-p352", "1.9.3-p125-perf"]
49
+
50
+ RUBY_VERSIONS.each do |ruby_version|
51
+ execute("rbenv local #{ruby_version}")
52
+ reset_bundle
53
+ run("Gemfile")
54
+ execute("rbenv local --unset")
55
+ end
56
+
57
+ RUBY_VERSIONS.each do |ruby_version|
48
58
  execute("rbenv local #{ruby_version}")
49
59
  ["test-unit", "minitest"].each do |test_library|
50
60
  reset_bundle
51
- Dir["gemfiles/Gemfile.#{test_library}.*"].each do |gemfile|
61
+ (Dir["gemfiles/Gemfile.#{test_library}.*"] + ["Gemfile"]).each do |gemfile|
52
62
  ruby_version_without_patch = ruby_version.split("-")[0]
53
63
  next if (ruby_version_without_patch == "1.9.3") && EXCLUDED_RUBY_193_GEMFILES.include?(gemfile)
64
+ next if (ruby_version_without_patch == "1.8.7") && (gemfile == "Gemfile") && (test_library == "minitest")
54
65
  p [ruby_version_without_patch, test_library, gemfile]
66
+ ENV['MOCHA_RUN_INTEGRATION_TESTS'] = test_library
55
67
  run(gemfile)
56
68
  end
57
69
  end
@@ -1,4 +1,8 @@
1
1
  require 'mocha/version'
2
2
  require 'mocha/integration'
3
- require 'mocha_standalone'
4
- require 'mocha/configuration'
3
+ require 'mocha/deprecation'
4
+
5
+ Mocha::Deprecation.warning("Change `require 'mocha'` to `require 'mocha/setup'`.")
6
+
7
+ require 'mocha/setup'
8
+
@@ -1,13 +1,25 @@
1
1
  require 'mocha/parameter_matchers'
2
+ require 'mocha/hooks'
2
3
  require 'mocha/mockery'
3
4
  require 'mocha/sequence'
5
+ require 'mocha/object_methods'
6
+ require 'mocha/module_methods'
7
+ require 'mocha/class_methods'
4
8
 
5
9
  module Mocha
6
10
 
7
- # Methods added to +Test::Unit::TestCase+ or equivalent.
11
+ # Methods added to +Test::Unit::TestCase+, +MiniTest::Unit::TestCase+ or equivalent.
8
12
  module API
9
13
 
10
14
  include ParameterMatchers
15
+ include Hooks
16
+
17
+ # @private
18
+ def self.included(mod)
19
+ Object.send(:include, Mocha::ObjectMethods)
20
+ Module.send(:include, Mocha::ModuleMethods)
21
+ Class.send(:include, Mocha::ClassMethods)
22
+ end
11
23
 
12
24
  # Builds a new mock object
13
25
  #
@@ -159,21 +171,6 @@ module Mocha
159
171
  Mockery.instance.new_state_machine(name)
160
172
  end
161
173
 
162
- # @private
163
- def mocha_setup
164
- end
165
-
166
- # @private
167
- def mocha_verify(assertion_counter = nil)
168
- Mockery.instance.verify(assertion_counter)
169
- end
170
-
171
- # @private
172
- def mocha_teardown
173
- Mockery.instance.teardown
174
- Mockery.reset_instance
175
- end
176
-
177
174
  end
178
175
 
179
176
  end
@@ -66,9 +66,13 @@ module Mocha
66
66
 
67
67
  def restore_original_method
68
68
  if @original_method && @original_method.owner == stubbee.__metaclass__
69
- original_method = @original_method
70
- stubbee.__metaclass__.send(:define_method, method) do |*args, &block|
71
- original_method.call(*args, &block)
69
+ if RUBY_VERSION < '1.9'
70
+ original_method = @original_method
71
+ stubbee.__metaclass__.send(:define_method, method) do |*args, &block|
72
+ original_method.call(*args, &block)
73
+ end
74
+ else
75
+ stubbee.__metaclass__.send(:define_method, method, @original_method)
72
76
  end
73
77
  stubbee.__metaclass__.send(@original_visibility, method)
74
78
  end
@@ -0,0 +1,63 @@
1
+ require 'mocha/mockery'
2
+ require 'mocha/class_method'
3
+ require 'mocha/any_instance_method'
4
+
5
+ module Mocha
6
+
7
+ # Methods added to all classes to allow mocking and stubbing on real (i.e. non-mock) objects.
8
+ module ClassMethods
9
+
10
+ # @private
11
+ def stubba_method
12
+ Mocha::ClassMethod
13
+ end
14
+
15
+ # @private
16
+ class AnyInstance
17
+
18
+ def initialize(klass)
19
+ @stubba_object = klass
20
+ end
21
+
22
+ def mocha
23
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
24
+ end
25
+
26
+ def stubba_method
27
+ Mocha::AnyInstanceMethod
28
+ end
29
+
30
+ def stubba_object
31
+ @stubba_object
32
+ end
33
+
34
+ def method_exists?(method, include_public_methods = true)
35
+ if include_public_methods
36
+ return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
37
+ end
38
+ return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
39
+ return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
40
+ return false
41
+ end
42
+
43
+ end
44
+
45
+ # @return [Mock] a mock object which will detect calls to any instance of this class.
46
+ # @raise [StubbingError] if attempting to stub method which is not allowed.
47
+ #
48
+ # @example Return false to invocation of +Product#save+ for any instance of +Product+.
49
+ # Product.any_instance.stubs(:save).returns(false)
50
+ # product_1 = Product.new
51
+ # assert_equal false, product_1.save
52
+ # product_2 = Product.new
53
+ # assert_equal false, product_2.save
54
+ def any_instance
55
+ if frozen?
56
+ raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}.any_instance", caller)
57
+ end
58
+ @any_instance ||= AnyInstance.new(self)
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,11 @@
1
+ module Mocha
2
+ module Debug
3
+ OPTIONS = (ENV['MOCHA_OPTIONS'] || '').split(',').inject({}) do |hash, key|
4
+ hash[key] = true; hash
5
+ end.freeze
6
+
7
+ def self.puts(message)
8
+ $stderr.puts(message) if OPTIONS['debug']
9
+ end
10
+ end
11
+ end
@@ -8,7 +8,7 @@ module Mocha
8
8
 
9
9
  def warning(message)
10
10
  @messages << message
11
- $stderr.puts "Mocha deprecation warning: #{message}" unless mode == :disabled
11
+ $stderr.puts "\n*** Mocha deprecation warning: #{message}\n\n" unless mode == :disabled
12
12
  $stderr.puts caller.join("\n ") if mode == :debug
13
13
  end
14
14
 
@@ -7,7 +7,7 @@ module Mocha
7
7
  end
8
8
 
9
9
  def evaluate
10
- raise @exception, @exception.to_s if @exception.is_a?(Module) && @exception.ancestors.include?(Interrupt)
10
+ raise @exception, @exception.to_s if @exception.is_a?(Module) && (@exception < Interrupt)
11
11
  raise @exception, @message if @message
12
12
  raise @exception
13
13
  end
@@ -1,3 +1,8 @@
1
1
  module Mocha
2
+ # Default exception class raised when an unexpected invocation or an unsatisfied expectation occurs.
3
+ #
4
+ # Authors of test libraries may use +Mocha::ExpectationErrorFactory+ to have Mocha raise a different exception.
5
+ #
6
+ # @see Mocha::ExpectationErrorFactory
2
7
  class ExpectationError < Exception; end
3
8
  end
@@ -2,9 +2,28 @@ require 'mocha/backtrace_filter'
2
2
  require 'mocha/expectation_error'
3
3
 
4
4
  module Mocha
5
+
6
+ # This factory determines what class of exception should be raised when Mocha detects a test failure.
7
+ #
8
+ # This class should only be used by authors of test libraries and not by typical "users" of Mocha.
9
+ #
10
+ # For example, it is used by +Mocha::Integration::MiniTest::Adapter+ in order to have Mocha raise a +MiniTest::Assertion+ which can then be sensibly handled by +MiniTest::Unit::TestCase+.
11
+ #
12
+ # @see Mocha::Integration::MiniTest::Adapter
5
13
  class ExpectationErrorFactory
6
14
  class << self
15
+ # @!attribute exception_class
16
+ # Determines what class of exception should be raised when Mocha detects a test failure.
17
+ #
18
+ # This attribute may be set by authors of test libraries in order to have Mocha raise exceptions of a specific class when there is an unexpected invocation or an unsatisfied expectation.
19
+ #
20
+ # By default a +Mocha::ExpectationError+ will be raised.
21
+ #
22
+ # @return [Exception] class of exception to be raised when an expectation error occurs
23
+ # @see Mocha::ExpectationError
7
24
  attr_accessor :exception_class
25
+
26
+ # @private
8
27
  def build(message = nil, backtrace = [])
9
28
  self.exception_class ||= ExpectationError
10
29
  exception = exception_class.new(message)
@@ -0,0 +1,43 @@
1
+ require 'mocha/mockery'
2
+
3
+ module Mocha
4
+
5
+ # Integration hooks for test library authors.
6
+ #
7
+ # The methods in this module should be called from test libraries wishing to integrate with Mocha.
8
+ #
9
+ # This module is provided as part of the +Mocha::API+ module and is therefore part of the public API, but should only be used by authors of test libraries and not by typical "users" of Mocha.
10
+ #
11
+ # Integration with Test::Unit and MiniTest are provided as part of Mocha, because they are (or were once) part of the Ruby standard library. Integration with other test libraries is not provided as *part* of Mocha, but is supported by means of the methods in this module.
12
+ #
13
+ # See the code in the +Adapter+ modules for examples of how to use the methods in this module. +Mocha::ExpectationErrorFactory+ may be used if you want +Mocha+ to raise a different type of exception.
14
+ #
15
+ # @see Mocha::Integration::TestUnit::Adapter
16
+ # @see Mocha::Integration::MiniTest::Adapter
17
+ # @see Mocha::ExpectationErrorFactory
18
+ # @see Mocha::API
19
+ module Hooks
20
+ # Prepares Mocha before a test (only for use by authors of test libraries).
21
+ #
22
+ # This method should be called before each individual test starts (including before any "setup" code).
23
+ def mocha_setup
24
+ end
25
+
26
+ # Verifies that all mock expectations have been met (only for use by authors of test libraries).
27
+ #
28
+ # This is equivalent to a series of "assertions".
29
+ #
30
+ # This method should be called at the end of each individual test, before it has been determined whether or not the test has passed.
31
+ def mocha_verify(assertion_counter = nil)
32
+ Mockery.instance.verify(assertion_counter)
33
+ end
34
+
35
+ # Resets Mocha after a test (only for use by authors of test libraries).
36
+ #
37
+ # This method should be called after each individual test has finished (including after any "teardown" code).
38
+ def mocha_teardown
39
+ Mockery.instance.teardown
40
+ Mockery.reset_instance
41
+ end
42
+ end
43
+ end
@@ -1,46 +1,14 @@
1
- require 'mocha/options'
2
1
  require 'mocha/deprecation'
2
+ require 'mocha/integration/test_unit'
3
+ require 'mocha/integration/mini_test'
3
4
 
4
5
  module Mocha
5
-
6
6
  module Integration
7
-
8
- class << self
9
-
10
- def monkey_patches
11
- patches = []
12
- if test_unit_testcase_defined? && !test_unit_testcase_inherits_from_miniunit_testcase?
13
- patches << 'mocha/integration/test_unit'
14
- end
15
- if mini_test_testcase_defined?
16
- patches << 'mocha/integration/mini_test'
17
- end
18
- patches
19
- end
20
-
21
- def test_unit_testcase_defined?
22
- defined?(Test::Unit::TestCase)
23
- end
24
-
25
- def mini_test_testcase_defined?
26
- defined?(MiniTest::Unit::TestCase)
27
- end
28
-
29
- def test_unit_testcase_inherits_from_miniunit_testcase?
30
- test_unit_testcase_defined? && mini_test_testcase_defined? && Test::Unit::TestCase.ancestors.include?(MiniTest::Unit::TestCase)
7
+ def self.activate
8
+ if [Integration::TestUnit, Integration::MiniTest].map(&:activate).none?
9
+ Deprecation.warning("Test::Unit or MiniTest must be loaded *before* Mocha.")
10
+ Deprecation.warning("If you're integrating with a test library other than Test::Unit or MiniTest, you should use `require 'mocha/api'` instead of `require 'mocha'`.")
31
11
  end
32
-
33
12
  end
34
-
35
13
  end
36
-
37
- end
38
-
39
- unless Mocha::Integration.monkey_patches.any?
40
- Mocha::Deprecation.warning("Test::Unit or MiniTest must be loaded *before* Mocha.")
41
- Mocha::Deprecation.warning("If you're integrating with another test library, you should probably require 'mocha_standalone' instead of 'mocha'")
42
- end
43
-
44
- Mocha::Integration.monkey_patches.each do |patch|
45
- require patch
46
14
  end