mocha 0.12.7 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. data/.yardopts +22 -0
  2. data/COPYING.md +3 -0
  3. data/README.md +230 -0
  4. data/{RELEASE.rdoc → RELEASE.md} +61 -47
  5. data/Rakefile +45 -37
  6. data/build-matrix.rb +16 -4
  7. data/lib/mocha/api.rb +13 -16
  8. data/lib/mocha/class_method.rb +7 -3
  9. data/lib/mocha/class_methods.rb +63 -0
  10. data/lib/mocha/debug.rb +11 -0
  11. data/lib/mocha/deprecation.rb +1 -1
  12. data/lib/mocha/exception_raiser.rb +1 -1
  13. data/lib/mocha/expectation_error.rb +5 -0
  14. data/lib/mocha/expectation_error_factory.rb +19 -0
  15. data/lib/mocha/hooks.rb +43 -0
  16. data/lib/mocha/integration/assertion_counter.rb +13 -0
  17. data/lib/mocha/integration/mini_test/adapter.rb +54 -0
  18. data/lib/mocha/integration/mini_test/exception_translation.rb +0 -6
  19. data/lib/mocha/integration/mini_test/nothing.rb +19 -0
  20. data/lib/mocha/integration/mini_test/version_13.rb +32 -25
  21. data/lib/mocha/integration/mini_test/version_140.rb +32 -26
  22. data/lib/mocha/integration/mini_test/version_141.rb +40 -34
  23. data/lib/mocha/integration/mini_test/version_142_to_172.rb +40 -34
  24. data/lib/mocha/integration/mini_test/version_200.rb +41 -35
  25. data/lib/mocha/integration/mini_test/version_201_to_222.rb +41 -35
  26. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +49 -43
  27. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +53 -47
  28. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +43 -37
  29. data/lib/mocha/integration/mini_test.rb +42 -51
  30. data/lib/mocha/integration/monkey_patcher.rb +18 -0
  31. data/lib/mocha/integration/test_unit/adapter.rb +51 -0
  32. data/lib/mocha/integration/test_unit/gem_version_200.rb +36 -29
  33. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +36 -29
  34. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +36 -29
  35. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +65 -0
  36. data/lib/mocha/integration/test_unit/nothing.rb +19 -0
  37. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +36 -29
  38. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +37 -30
  39. data/lib/mocha/integration/test_unit.rb +38 -45
  40. data/lib/mocha/integration.rb +6 -38
  41. data/lib/mocha/method_matcher.rb +1 -1
  42. data/lib/mocha/module_methods.rb +14 -0
  43. data/lib/mocha/{object.rb → object_methods.rb} +0 -83
  44. data/lib/mocha/setup.rb +10 -0
  45. data/lib/mocha/standalone.rb +3 -0
  46. data/lib/mocha/version.rb +1 -1
  47. data/lib/mocha.rb +6 -2
  48. data/lib/mocha_standalone.rb +3 -1
  49. data/mocha.gemspec +16 -9
  50. data/test/acceptance/bug_18914_test.rb +1 -1
  51. data/test/acceptance/bug_21465_test.rb +1 -1
  52. data/test/acceptance/bug_21563_test.rb +1 -1
  53. data/test/acceptance/exception_rescue_test.rb +1 -1
  54. data/test/acceptance/expectations_on_multiple_methods_test.rb +1 -1
  55. data/test/acceptance/expected_invocation_count_test.rb +1 -1
  56. data/test/acceptance/failure_messages_test.rb +1 -1
  57. data/test/acceptance/issue_65_test.rb +1 -1
  58. data/test/acceptance/issue_70_test.rb +1 -1
  59. data/test/acceptance/mocha_example_test.rb +1 -1
  60. data/test/acceptance/mocha_test_result_test.rb +1 -1
  61. data/test/acceptance/mock_test.rb +1 -1
  62. data/test/acceptance/mock_with_initializer_block_test.rb +1 -1
  63. data/test/acceptance/mocked_methods_dispatch_test.rb +1 -1
  64. data/test/acceptance/multiple_expectations_failure_message_test.rb +1 -1
  65. data/test/acceptance/optional_parameters_test.rb +1 -1
  66. data/test/acceptance/parameter_matcher_test.rb +1 -1
  67. data/test/acceptance/partial_mocks_test.rb +1 -1
  68. data/test/acceptance/raise_exception_test.rb +1 -1
  69. data/test/acceptance/return_value_test.rb +1 -1
  70. data/test/acceptance/sequence_test.rb +1 -1
  71. data/test/acceptance/states_test.rb +1 -1
  72. data/test/acceptance/stub_any_instance_method_test.rb +1 -1
  73. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +1 -1
  74. data/test/acceptance/stub_class_method_defined_on_class_test.rb +1 -1
  75. data/test/acceptance/stub_class_method_defined_on_module_test.rb +1 -1
  76. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -1
  77. data/test/acceptance/stub_everything_test.rb +1 -1
  78. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +1 -1
  79. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +1 -1
  80. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +1 -1
  81. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +1 -1
  82. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +1 -1
  83. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +1 -1
  84. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
  85. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +1 -1
  86. data/test/acceptance/stub_module_method_test.rb +1 -1
  87. data/test/acceptance/stub_test.rb +1 -1
  88. data/test/acceptance/stubba_example_test.rb +1 -1
  89. data/test/acceptance/stubba_test_result_test.rb +1 -1
  90. data/test/acceptance/stubbing_error_backtrace_test.rb +1 -1
  91. data/test/acceptance/stubbing_frozen_object_test.rb +1 -1
  92. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +1 -1
  93. data/test/acceptance/stubbing_method_unnecessarily_test.rb +1 -1
  94. data/test/acceptance/stubbing_nil_test.rb +1 -1
  95. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +1 -1
  96. data/test/acceptance/stubbing_non_existent_class_method_test.rb +1 -1
  97. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +1 -1
  98. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +1 -1
  99. data/test/acceptance/stubbing_non_public_class_method_test.rb +1 -1
  100. data/test/acceptance/stubbing_non_public_instance_method_test.rb +1 -1
  101. data/test/acceptance/stubbing_on_non_mock_object_test.rb +1 -1
  102. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +3 -3
  103. data/test/acceptance/throw_test.rb +1 -1
  104. data/test/acceptance/unstubbing_test.rb +1 -1
  105. data/test/integration/mini_test_test.rb +9 -0
  106. data/test/integration/shared_tests.rb +174 -0
  107. data/test/integration/test_unit_test.rb +9 -0
  108. data/test/mini_test_result.rb +0 -1
  109. data/test/test_runner.rb +14 -14
  110. data/test/unit/class_methods_test.rb +40 -0
  111. data/test/unit/method_matcher_test.rb +5 -0
  112. data/test/unit/mock_test.rb +6 -0
  113. data/test/unit/module_methods_test.rb +19 -0
  114. data/test/unit/object_methods_test.rb +45 -0
  115. metadata +43 -42
  116. data/COPYING.rdoc +0 -3
  117. data/README.rdoc +0 -56
  118. data/examples/misc.rb +0 -43
  119. data/examples/mocha.rb +0 -25
  120. data/examples/stubba.rb +0 -64
  121. data/lib/mocha/integration/mini_test/assertion_counter.rb +0 -23
  122. data/lib/mocha/integration/mini_test/version_330_to_410.rb +0 -65
  123. data/lib/mocha/integration/test_unit/assertion_counter.rb +0 -23
  124. data/lib/mocha/integration/test_unit/gem_version_230_to_252.rb +0 -58
  125. data/lib/mocha/options.rb +0 -1
  126. data/test/acceptance/api_test.rb +0 -139
  127. data/test/acceptance/minitest_test.rb +0 -162
  128. data/test/unit/object_test.rb +0 -92
  129. /data/{MIT-LICENSE.rdoc → MIT-LICENSE.md} +0 -0
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
data/build-matrix.rb CHANGED
@@ -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
data/lib/mocha/api.rb CHANGED
@@ -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
@@ -0,0 +1,13 @@
1
+ module Mocha
2
+ module Integration
3
+ class AssertionCounter
4
+ def initialize(test_case)
5
+ @test_case = test_case
6
+ end
7
+
8
+ def increment
9
+ @test_case.assert(true)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,54 @@
1
+ require 'mocha/api'
2
+ require 'mocha/integration/assertion_counter'
3
+ require 'mocha/expectation_error_factory'
4
+
5
+ module Mocha
6
+ module Integration
7
+ module MiniTest
8
+
9
+ # Integrates Mocha into recent versions of MiniTest.
10
+ #
11
+ # See the source code for an example of how to integrate Mocha into a test library.
12
+ module Adapter
13
+ include Mocha::API
14
+
15
+ # @private
16
+ def self.applicable_to?(mini_test_version)
17
+ Gem::Requirement.new('>= 3.3.0').satisfied_by?(mini_test_version)
18
+ end
19
+
20
+ # @private
21
+ def self.description
22
+ "adapter for MiniTest gem >= v3.3.0"
23
+ end
24
+
25
+ # @private
26
+ def self.included(mod)
27
+ Mocha::ExpectationErrorFactory.exception_class = ::MiniTest::Assertion
28
+ end
29
+
30
+ # @private
31
+ def before_setup
32
+ mocha_setup
33
+ super
34
+ end
35
+
36
+ # @private
37
+ def before_teardown
38
+ return unless passed?
39
+ assertion_counter = Integration::AssertionCounter.new(self)
40
+ mocha_verify(assertion_counter)
41
+ ensure
42
+ super
43
+ end
44
+
45
+ # @private
46
+ def after_teardown
47
+ super
48
+ mocha_teardown
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
@@ -1,20 +1,14 @@
1
1
  require 'mocha/expectation_error'
2
2
 
3
3
  module Mocha
4
-
5
4
  module Integration
6
-
7
5
  module MiniTest
8
-
9
6
  def self.translate(exception)
10
7
  return exception unless exception.kind_of?(::Mocha::ExpectationError)
11
8
  translated_exception = ::MiniTest::Assertion.new(exception.message)
12
9
  translated_exception.set_backtrace(exception.backtrace)
13
10
  translated_exception
14
11
  end
15
-
16
12
  end
17
-
18
13
  end
19
-
20
14
  end
@@ -0,0 +1,19 @@
1
+ module Mocha
2
+ module Integration
3
+ module MiniTest
4
+ module Nothing
5
+ def self.applicable_to?(test_unit_version, ruby_version = nil)
6
+ true
7
+ end
8
+
9
+ def self.description
10
+ "nothing (no MiniTest integration available)"
11
+ end
12
+
13
+ def self.included(mod)
14
+ raise "No MiniTest integration available"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,44 +1,51 @@
1
- require 'mocha/integration/mini_test/assertion_counter'
1
+ require 'mocha/integration/assertion_counter'
2
+ require 'mocha/integration/monkey_patcher'
3
+ require 'mocha/integration/mini_test/exception_translation'
2
4
 
3
5
  module Mocha
4
-
5
6
  module Integration
6
-
7
7
  module MiniTest
8
-
9
8
  module Version13
9
+ def self.applicable_to?(mini_test_version)
10
+ Gem::Requirement.new('>= 1.3.0', '<= 1.3.1').satisfied_by?(mini_test_version)
11
+ end
12
+
13
+ def self.description
14
+ "monkey patch for MiniTest gem v1.3"
15
+ end
16
+
10
17
  def self.included(mod)
11
- $stderr.puts "Monkey patching MiniTest v1.3" if $mocha_options['debug']
18
+ MonkeyPatcher.apply(mod, RunMethodPatch)
12
19
  end
13
- def run runner
14
- assertion_counter = AssertionCounter.new(self)
15
- result = '.'
16
- begin
20
+
21
+ module RunMethodPatch
22
+ def run runner
23
+ assertion_counter = AssertionCounter.new(self)
24
+ result = '.'
17
25
  begin
18
- @passed = nil
19
- self.setup
20
- self.__send__ self.name
21
- mocha_verify(assertion_counter)
22
- @passed = true
23
- rescue Exception => e
24
- @passed = false
25
- result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
26
- ensure
27
26
  begin
28
- self.teardown
27
+ @passed = nil
28
+ self.setup
29
+ self.__send__ self.name
30
+ mocha_verify(assertion_counter)
31
+ @passed = true
29
32
  rescue Exception => e
33
+ @passed = false
30
34
  result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
35
+ ensure
36
+ begin
37
+ self.teardown
38
+ rescue Exception => e
39
+ result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
40
+ end
31
41
  end
42
+ ensure
43
+ mocha_teardown
32
44
  end
33
- ensure
34
- mocha_teardown
45
+ result
35
46
  end
36
- result
37
47
  end
38
48
  end
39
-
40
49
  end
41
-
42
50
  end
43
-
44
51
  end
@@ -1,45 +1,51 @@
1
- require 'mocha/integration/mini_test/assertion_counter'
2
- require 'mocha/expectation_error'
1
+ require 'mocha/integration/assertion_counter'
2
+ require 'mocha/integration/monkey_patcher'
3
+ require 'mocha/integration/mini_test/exception_translation'
3
4
 
4
5
  module Mocha
5
-
6
6
  module Integration
7
-
8
7
  module MiniTest
9
-
10
8
  module Version140
9
+ def self.applicable_to?(mini_test_version)
10
+ Gem::Requirement.new('1.4.0').satisfied_by?(mini_test_version)
11
+ end
12
+
13
+ def self.description
14
+ "monkey patch for MiniTest gem v1.4.0"
15
+ end
16
+
11
17
  def self.included(mod)
12
- $stderr.puts "Monkey patching MiniTest v1.4.0" if $mocha_options['debug']
18
+ MonkeyPatcher.apply(mod, RunMethodPatch)
13
19
  end
14
- def run runner
15
- assertion_counter = AssertionCounter.new(self)
16
- result = '.'
17
- begin
20
+
21
+ module RunMethodPatch
22
+ def run runner
23
+ assertion_counter = AssertionCounter.new(self)
24
+ result = '.'
18
25
  begin
19
- @passed = nil
20
- self.setup
21
- self.__send__ self.__name__
22
- mocha_verify(assertion_counter)
23
- @passed = true
24
- rescue Exception => e
25
- @passed = false
26
- result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
27
- ensure
28
26
  begin
29
- self.teardown
27
+ @passed = nil
28
+ self.setup
29
+ self.__send__ self.__name__
30
+ mocha_verify(assertion_counter)
31
+ @passed = true
30
32
  rescue Exception => e
33
+ @passed = false
31
34
  result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
35
+ ensure
36
+ begin
37
+ self.teardown
38
+ rescue Exception => e
39
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
40
+ end
32
41
  end
42
+ ensure
43
+ mocha_teardown
33
44
  end
34
- ensure
35
- mocha_teardown
45
+ result
36
46
  end
37
- result
38
47
  end
39
48
  end
40
-
41
49
  end
42
-
43
50
  end
44
-
45
51
  end