jferris-mocha 0.9.5.0.1240002286

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 (180) hide show
  1. data/COPYING +3 -0
  2. data/MIT-LICENSE +7 -0
  3. data/README +37 -0
  4. data/RELEASE +269 -0
  5. data/Rakefile +217 -0
  6. data/examples/misc.rb +43 -0
  7. data/examples/mocha.rb +25 -0
  8. data/examples/stubba.rb +64 -0
  9. data/lib/mocha/any_instance_method.rb +55 -0
  10. data/lib/mocha/api.rb +232 -0
  11. data/lib/mocha/argument_iterator.rb +21 -0
  12. data/lib/mocha/backtrace_filter.rb +17 -0
  13. data/lib/mocha/cardinality.rb +95 -0
  14. data/lib/mocha/central.rb +27 -0
  15. data/lib/mocha/change_state_side_effect.rb +19 -0
  16. data/lib/mocha/class_method.rb +87 -0
  17. data/lib/mocha/configuration.rb +60 -0
  18. data/lib/mocha/deprecation.rb +22 -0
  19. data/lib/mocha/exception_raiser.rb +17 -0
  20. data/lib/mocha/expectation.rb +484 -0
  21. data/lib/mocha/expectation_error.rb +15 -0
  22. data/lib/mocha/expectation_list.rb +50 -0
  23. data/lib/mocha/in_state_ordering_constraint.rb +19 -0
  24. data/lib/mocha/inspect.rb +67 -0
  25. data/lib/mocha/instance_method.rb +16 -0
  26. data/lib/mocha/integration/bacon/assertion_counter.rb +23 -0
  27. data/lib/mocha/integration/bacon/version_11_and_above.rb +34 -0
  28. data/lib/mocha/integration/bacon.rb +1 -0
  29. data/lib/mocha/integration/mini_test/assertion_counter.rb +23 -0
  30. data/lib/mocha/integration/mini_test/version_131_and_above.rb +49 -0
  31. data/lib/mocha/integration/mini_test.rb +1 -0
  32. data/lib/mocha/integration/test_unit/assertion_counter.rb +23 -0
  33. data/lib/mocha/integration/test_unit/gem_version_200.rb +49 -0
  34. data/lib/mocha/integration/test_unit/gem_version_201_and_above.rb +49 -0
  35. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +48 -0
  36. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +50 -0
  37. data/lib/mocha/integration/test_unit.rb +4 -0
  38. data/lib/mocha/invocation.rb +10 -0
  39. data/lib/mocha/is_a.rb +9 -0
  40. data/lib/mocha/logger.rb +15 -0
  41. data/lib/mocha/metaclass.rb +13 -0
  42. data/lib/mocha/method_matcher.rb +21 -0
  43. data/lib/mocha/mock.rb +200 -0
  44. data/lib/mocha/mockery.rb +189 -0
  45. data/lib/mocha/module_method.rb +16 -0
  46. data/lib/mocha/multiple_yields.rb +20 -0
  47. data/lib/mocha/names.rb +53 -0
  48. data/lib/mocha/no_yields.rb +11 -0
  49. data/lib/mocha/object.rb +187 -0
  50. data/lib/mocha/parameter_matchers/all_of.rb +42 -0
  51. data/lib/mocha/parameter_matchers/any_of.rb +47 -0
  52. data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
  53. data/lib/mocha/parameter_matchers/anything.rb +33 -0
  54. data/lib/mocha/parameter_matchers/base.rb +15 -0
  55. data/lib/mocha/parameter_matchers/equals.rb +42 -0
  56. data/lib/mocha/parameter_matchers/has_entries.rb +45 -0
  57. data/lib/mocha/parameter_matchers/has_entry.rb +57 -0
  58. data/lib/mocha/parameter_matchers/has_key.rb +42 -0
  59. data/lib/mocha/parameter_matchers/has_value.rb +42 -0
  60. data/lib/mocha/parameter_matchers/includes.rb +40 -0
  61. data/lib/mocha/parameter_matchers/instance_of.rb +42 -0
  62. data/lib/mocha/parameter_matchers/is_a.rb +42 -0
  63. data/lib/mocha/parameter_matchers/kind_of.rb +42 -0
  64. data/lib/mocha/parameter_matchers/not.rb +42 -0
  65. data/lib/mocha/parameter_matchers/object.rb +15 -0
  66. data/lib/mocha/parameter_matchers/optionally.rb +55 -0
  67. data/lib/mocha/parameter_matchers/regexp_matches.rb +43 -0
  68. data/lib/mocha/parameter_matchers/responds_with.rb +43 -0
  69. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +43 -0
  70. data/lib/mocha/parameter_matchers.rb +27 -0
  71. data/lib/mocha/parameters_matcher.rb +37 -0
  72. data/lib/mocha/pretty_parameters.rb +28 -0
  73. data/lib/mocha/return_values.rb +31 -0
  74. data/lib/mocha/sequence.rb +42 -0
  75. data/lib/mocha/single_return_value.rb +17 -0
  76. data/lib/mocha/single_yield.rb +18 -0
  77. data/lib/mocha/state_machine.rb +91 -0
  78. data/lib/mocha/stubbing_error.rb +16 -0
  79. data/lib/mocha/unexpected_invocation.rb +18 -0
  80. data/lib/mocha/yield_parameters.rb +31 -0
  81. data/lib/mocha.rb +69 -0
  82. data/lib/mocha_standalone.rb +2 -0
  83. data/lib/stubba.rb +4 -0
  84. data/test/acceptance/acceptance_test_helper.rb +38 -0
  85. data/test/acceptance/api_test.rb +139 -0
  86. data/test/acceptance/bacon_spec.rb +67 -0
  87. data/test/acceptance/bacon_test.rb +110 -0
  88. data/test/acceptance/bug_18914_test.rb +43 -0
  89. data/test/acceptance/bug_21465_test.rb +34 -0
  90. data/test/acceptance/bug_21563_test.rb +25 -0
  91. data/test/acceptance/expected_invocation_count_test.rb +196 -0
  92. data/test/acceptance/failure_messages_test.rb +64 -0
  93. data/test/acceptance/minitest_test.rb +146 -0
  94. data/test/acceptance/mocha_example_test.rb +98 -0
  95. data/test/acceptance/mocha_test_result_test.rb +84 -0
  96. data/test/acceptance/mock_test.rb +100 -0
  97. data/test/acceptance/mock_with_initializer_block_test.rb +51 -0
  98. data/test/acceptance/mocked_methods_dispatch_test.rb +78 -0
  99. data/test/acceptance/optional_parameters_test.rb +70 -0
  100. data/test/acceptance/parameter_matcher_test.rb +209 -0
  101. data/test/acceptance/partial_mocks_test.rb +47 -0
  102. data/test/acceptance/return_value_test.rb +52 -0
  103. data/test/acceptance/sequence_test.rb +186 -0
  104. data/test/acceptance/spy_test.rb +109 -0
  105. data/test/acceptance/states_test.rb +70 -0
  106. data/test/acceptance/stub_any_instance_method_test.rb +195 -0
  107. data/test/acceptance/stub_class_method_test.rb +203 -0
  108. data/test/acceptance/stub_everything_test.rb +56 -0
  109. data/test/acceptance/stub_instance_method_test.rb +203 -0
  110. data/test/acceptance/stub_module_method_test.rb +163 -0
  111. data/test/acceptance/stub_test.rb +52 -0
  112. data/test/acceptance/stubba_example_test.rb +102 -0
  113. data/test/acceptance/stubba_test.rb +15 -0
  114. data/test/acceptance/stubba_test_result_test.rb +66 -0
  115. data/test/acceptance/stubbing_error_backtrace_test.rb +64 -0
  116. data/test/acceptance/stubbing_method_unnecessarily_test.rb +65 -0
  117. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +130 -0
  118. data/test/acceptance/stubbing_non_existent_class_method_test.rb +157 -0
  119. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +147 -0
  120. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +130 -0
  121. data/test/acceptance/stubbing_non_public_class_method_test.rb +163 -0
  122. data/test/acceptance/stubbing_non_public_instance_method_test.rb +143 -0
  123. data/test/acceptance/stubbing_on_non_mock_object_test.rb +64 -0
  124. data/test/deprecation_disabler.rb +15 -0
  125. data/test/execution_point.rb +36 -0
  126. data/test/matcher_helpers.rb +5 -0
  127. data/test/method_definer.rb +24 -0
  128. data/test/simple_counter.rb +13 -0
  129. data/test/test_helper.rb +16 -0
  130. data/test/test_runner.rb +33 -0
  131. data/test/unit/any_instance_method_test.rb +126 -0
  132. data/test/unit/array_inspect_test.rb +16 -0
  133. data/test/unit/assert_received_test.rb +136 -0
  134. data/test/unit/backtrace_filter_test.rb +19 -0
  135. data/test/unit/cardinality_test.rb +56 -0
  136. data/test/unit/central_test.rb +65 -0
  137. data/test/unit/change_state_side_effect_test.rb +41 -0
  138. data/test/unit/class_method_test.rb +237 -0
  139. data/test/unit/date_time_inspect_test.rb +21 -0
  140. data/test/unit/exception_raiser_test.rb +42 -0
  141. data/test/unit/expectation_list_test.rb +57 -0
  142. data/test/unit/expectation_test.rb +526 -0
  143. data/test/unit/hash_inspect_test.rb +16 -0
  144. data/test/unit/have_received_test.rb +137 -0
  145. data/test/unit/in_state_ordering_constraint_test.rb +43 -0
  146. data/test/unit/invocation_test.rb +17 -0
  147. data/test/unit/metaclass_test.rb +22 -0
  148. data/test/unit/method_matcher_test.rb +23 -0
  149. data/test/unit/mock_test.rb +329 -0
  150. data/test/unit/mockery_test.rb +163 -0
  151. data/test/unit/multiple_yields_test.rb +18 -0
  152. data/test/unit/no_yields_test.rb +18 -0
  153. data/test/unit/object_inspect_test.rb +37 -0
  154. data/test/unit/object_test.rb +82 -0
  155. data/test/unit/parameter_matchers/all_of_test.rb +26 -0
  156. data/test/unit/parameter_matchers/any_of_test.rb +26 -0
  157. data/test/unit/parameter_matchers/anything_test.rb +21 -0
  158. data/test/unit/parameter_matchers/equals_test.rb +25 -0
  159. data/test/unit/parameter_matchers/has_entries_test.rb +51 -0
  160. data/test/unit/parameter_matchers/has_entry_test.rb +82 -0
  161. data/test/unit/parameter_matchers/has_key_test.rb +36 -0
  162. data/test/unit/parameter_matchers/has_value_test.rb +37 -0
  163. data/test/unit/parameter_matchers/includes_test.rb +25 -0
  164. data/test/unit/parameter_matchers/instance_of_test.rb +25 -0
  165. data/test/unit/parameter_matchers/is_a_test.rb +25 -0
  166. data/test/unit/parameter_matchers/kind_of_test.rb +25 -0
  167. data/test/unit/parameter_matchers/not_test.rb +26 -0
  168. data/test/unit/parameter_matchers/regexp_matches_test.rb +25 -0
  169. data/test/unit/parameter_matchers/responds_with_test.rb +25 -0
  170. data/test/unit/parameter_matchers/stub_matcher.rb +27 -0
  171. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +25 -0
  172. data/test/unit/parameters_matcher_test.rb +121 -0
  173. data/test/unit/return_values_test.rb +63 -0
  174. data/test/unit/sequence_test.rb +104 -0
  175. data/test/unit/single_return_value_test.rb +14 -0
  176. data/test/unit/single_yield_test.rb +18 -0
  177. data/test/unit/state_machine_test.rb +98 -0
  178. data/test/unit/string_inspect_test.rb +11 -0
  179. data/test/unit/yield_parameters_test.rb +93 -0
  180. metadata +245 -0
@@ -0,0 +1,28 @@
1
+ require 'mocha/inspect'
2
+
3
+ module Mocha
4
+
5
+ class PrettyParameters
6
+
7
+ def initialize(params)
8
+ @params = params
9
+ @params_string = params.mocha_inspect
10
+ end
11
+
12
+ def pretty
13
+ remove_outer_array_braces!
14
+ remove_outer_hash_braces!
15
+ @params_string
16
+ end
17
+
18
+ def remove_outer_array_braces!
19
+ @params_string = @params_string.gsub(/^\[|\]$/, '')
20
+ end
21
+
22
+ def remove_outer_hash_braces!
23
+ @params_string = @params_string.gsub(/^\{|\}$/, '') if @params.length == 1
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'mocha/single_return_value'
2
+
3
+ module Mocha # :nodoc:
4
+
5
+ class ReturnValues # :nodoc:
6
+
7
+ def self.build(*values)
8
+ new(*values.map { |value| SingleReturnValue.new(value) })
9
+ end
10
+
11
+ attr_accessor :values
12
+
13
+ def initialize(*values)
14
+ @values = values
15
+ end
16
+
17
+ def next
18
+ case @values.length
19
+ when 0 then nil
20
+ when 1 then @values.first.evaluate
21
+ else @values.shift.evaluate
22
+ end
23
+ end
24
+
25
+ def +(other)
26
+ self.class.new(*(@values + other.values))
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,42 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class Sequence
4
+
5
+ class InSequenceOrderingConstraint
6
+
7
+ def initialize(sequence, index)
8
+ @sequence, @index = sequence, index
9
+ end
10
+
11
+ def allows_invocation_now?
12
+ @sequence.satisfied_to_index?(@index)
13
+ end
14
+
15
+ def mocha_inspect
16
+ "in sequence #{@sequence.mocha_inspect}"
17
+ end
18
+
19
+ end
20
+
21
+ def initialize(name)
22
+ @name = name
23
+ @expectations = []
24
+ end
25
+
26
+ def constrain_as_next_in_sequence(expectation)
27
+ index = @expectations.length
28
+ @expectations << expectation
29
+ expectation.add_ordering_constraint(InSequenceOrderingConstraint.new(self, index))
30
+ end
31
+
32
+ def satisfied_to_index?(index)
33
+ @expectations[0...index].all? { |expectation| expectation.satisfied? }
34
+ end
35
+
36
+ def mocha_inspect
37
+ "#{@name.mocha_inspect}"
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,17 @@
1
+ require 'mocha/is_a'
2
+
3
+ module Mocha # :nodoc:
4
+
5
+ class SingleReturnValue # :nodoc:
6
+
7
+ def initialize(value)
8
+ @value = value
9
+ end
10
+
11
+ def evaluate
12
+ @value
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,18 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class SingleYield # :nodoc:
4
+
5
+ attr_reader :parameters
6
+
7
+ def initialize(*parameters)
8
+ @parameters = parameters
9
+ end
10
+
11
+ def each
12
+ yield(@parameters)
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
@@ -0,0 +1,91 @@
1
+ module Mocha # :nodoc:
2
+
3
+ # A state machine that is used to constrain the order of invocations.
4
+ # An invocation can be constrained to occur when a state is, or is_not, active.
5
+ class StateMachine
6
+
7
+ class State # :nodoc:
8
+
9
+ def initialize(state_machine, state)
10
+ @state_machine, @state = state_machine, state
11
+ end
12
+
13
+ def activate
14
+ @state_machine.current_state = @state
15
+ end
16
+
17
+ def active?
18
+ @state_machine.current_state == @state
19
+ end
20
+
21
+ def mocha_inspect
22
+ "#{@state_machine.name} is #{@state.mocha_inspect}"
23
+ end
24
+
25
+ end
26
+
27
+ class StatePredicate # :nodoc:
28
+
29
+ def initialize(state_machine, state)
30
+ @state_machine, @state = state_machine, state
31
+ end
32
+
33
+ def active?
34
+ @state_machine.current_state != @state
35
+ end
36
+
37
+ def mocha_inspect
38
+ "#{@state_machine.name} is not #{@state.mocha_inspect}"
39
+ end
40
+
41
+ end
42
+
43
+ attr_reader :name # :nodoc:
44
+
45
+ attr_accessor :current_state # :nodoc:
46
+
47
+ def initialize(name) # :nodoc:
48
+ @name = name
49
+ @current_state = nil
50
+ end
51
+
52
+ # :call-seq: starts_as(initial_state) -> state_machine
53
+ #
54
+ # Put the +state_machine+ into the +initial_state+.
55
+ def starts_as(initial_state)
56
+ become(initial_state)
57
+ self
58
+ end
59
+
60
+ # :call-seq: become(next_state)
61
+ #
62
+ # Put the +state_machine+ into the +next_state+.
63
+ def become(next_state)
64
+ @current_state = next_state
65
+ end
66
+
67
+ # :call-seq: is(state)
68
+ #
69
+ # Determines whether the +state_machine+ is in the specified +state+.
70
+ def is(state)
71
+ State.new(self, state)
72
+ end
73
+
74
+ # :call-seq: is_not(state)
75
+ #
76
+ # Determines whether the +state_machine+ is not in the specified +state+.
77
+ def is_not(state)
78
+ StatePredicate.new(self, state)
79
+ end
80
+
81
+ def mocha_inspect # :nodoc:
82
+ if @current_state
83
+ "#{@name} is #{@current_state.mocha_inspect}"
84
+ else
85
+ "#{@name} has no current state"
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,16 @@
1
+ require 'mocha/backtrace_filter'
2
+
3
+ module Mocha # :nodoc:
4
+
5
+ # Exception raised when an action prevented by Configuration#prevent is attempted.
6
+ class StubbingError < StandardError
7
+
8
+ def initialize(message = nil, backtrace = []) # :nodoc:
9
+ super(message)
10
+ filter = BacktraceFilter.new
11
+ set_backtrace(filter.filtered(backtrace))
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,18 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class UnexpectedInvocation
4
+
5
+ def initialize(mock, symbol, *arguments)
6
+ @mock = mock
7
+ @method_matcher = MethodMatcher.new(symbol)
8
+ @parameters_matcher = ParametersMatcher.new(arguments)
9
+ end
10
+
11
+ def to_s
12
+ method_signature = "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
13
+ "unexpected invocation: #{method_signature}\n"
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,31 @@
1
+ require 'mocha/no_yields'
2
+ require 'mocha/single_yield'
3
+ require 'mocha/multiple_yields'
4
+
5
+ module Mocha # :nodoc:
6
+
7
+ class YieldParameters # :nodoc:
8
+
9
+ def initialize
10
+ @parameter_groups = []
11
+ end
12
+
13
+ def next_invocation
14
+ case @parameter_groups.length
15
+ when 0 then NoYields.new
16
+ when 1 then @parameter_groups.first
17
+ else @parameter_groups.shift
18
+ end
19
+ end
20
+
21
+ def add(*parameters)
22
+ @parameter_groups << SingleYield.new(*parameters)
23
+ end
24
+
25
+ def multiple_add(*parameter_groups)
26
+ @parameter_groups << MultipleYields.new(*parameter_groups)
27
+ end
28
+
29
+ end
30
+
31
+ end
data/lib/mocha.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'mocha_standalone'
2
+ require 'mocha/configuration'
3
+
4
+ if defined?(MiniTest::Unit::TestCase) && !MiniTest::Unit::TestCase.ancestors.include?(Mocha::API)
5
+ require 'mocha/integration/mini_test'
6
+
7
+ module MiniTest
8
+ class Unit
9
+ class TestCase
10
+
11
+ include Mocha::API
12
+
13
+ alias_method :run_before_mocha, :run
14
+ remove_method :run
15
+
16
+ include Mocha::Integration::MiniTest::Version131AndAbove
17
+
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ if defined?(Bacon) && Bacon::VERSION >= "1.1" && defined?(Bacon::Context) && !Bacon::Context.ancestors.include?(Mocha::API)
24
+
25
+ require 'mocha/integration/bacon'
26
+
27
+ module Bacon
28
+ class Context
29
+ include Mocha::API
30
+ include Mocha::Integration::Bacon::Version11AndAbove
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ require 'test/unit/testcase'
37
+ require 'mocha/integration/test_unit'
38
+
39
+ unless Test::Unit::TestCase.ancestors.include?(Mocha::API)
40
+ module Test
41
+ module Unit
42
+ class TestCase
43
+
44
+ include Mocha::API
45
+
46
+ alias_method :run_before_mocha, :run
47
+ remove_method :run
48
+
49
+ test_unit_version = begin
50
+ require 'test/unit/version'
51
+ Test::Unit::VERSION
52
+ rescue LoadError
53
+ '1.x'
54
+ end
55
+
56
+ if test_unit_version == '2.0.0'
57
+ include Mocha::Integration::TestUnit::GemVersion200
58
+ elsif test_unit_version >= '2.0.1'
59
+ include Mocha::Integration::TestUnit::GemVersion201AndAbove
60
+ elsif RUBY_VERSION < '1.8.6'
61
+ include Mocha::Integration::TestUnit::RubyVersion185AndBelow
62
+ else
63
+ include Mocha::Integration::TestUnit::RubyVersion186AndAbove
64
+ end
65
+
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,2 @@
1
+ require 'mocha/api'
2
+ require 'mocha/object'
data/lib/stubba.rb ADDED
@@ -0,0 +1,4 @@
1
+ # for backwards compatibility
2
+ require 'mocha'
3
+ require 'mocha/deprecation'
4
+ Mocha::Deprecation.warning "require 'stubba' is no longer needed and stubba.rb will soon be removed"
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'test_runner'
3
+ require 'mocha/configuration'
4
+
5
+ module AcceptanceTest
6
+
7
+ class FakeLogger
8
+
9
+ attr_reader :warnings
10
+
11
+ def initialize
12
+ @warnings = []
13
+ end
14
+
15
+ def warn(message)
16
+ @warnings << message
17
+ end
18
+
19
+ end
20
+
21
+ attr_reader :logger
22
+
23
+ include TestRunner
24
+
25
+ def setup_acceptance_test
26
+ Mocha::Configuration.reset_configuration
27
+ @logger = FakeLogger.new
28
+ mockery = Mocha::Mockery.instance
29
+ @original_logger = mockery.logger
30
+ mockery.logger = @logger
31
+ end
32
+
33
+ def teardown_acceptance_test
34
+ Mocha::Configuration.reset_configuration
35
+ Mocha::Mockery.instance.logger = @original_logger
36
+ end
37
+
38
+ end
@@ -0,0 +1,139 @@
1
+ require File.join(File.dirname(__FILE__), "acceptance_test_helper")
2
+ require 'mocha_standalone'
3
+ require 'simple_counter'
4
+
5
+ class NotATestUnitAssertionFailedError < StandardError
6
+ end
7
+
8
+ class NotATestUnitTestCase
9
+
10
+ include Mocha::API
11
+
12
+ attr_reader :assertion_counter
13
+
14
+ def initialize
15
+ @assertion_counter = SimpleCounter.new
16
+ end
17
+
18
+ def run(test_method)
19
+ mocha_setup
20
+ begin
21
+ prepare
22
+ begin
23
+ send(test_method)
24
+ mocha_verify(@assertion_counter)
25
+ rescue Mocha::ExpectationError => e
26
+ new_error = NotATestUnitAssertionFailedError.new(e.message)
27
+ new_error.set_backtrace(e.backtrace)
28
+ raise new_error
29
+ ensure
30
+ cleanup
31
+ end
32
+ ensure
33
+ mocha_teardown
34
+ end
35
+ end
36
+
37
+ def prepare
38
+ end
39
+
40
+ def cleanup
41
+ end
42
+
43
+ end
44
+
45
+ class SampleTest < NotATestUnitTestCase
46
+
47
+ def mocha_with_fulfilled_expectation
48
+ mockee = mock()
49
+ mockee.expects(:blah)
50
+ mockee.blah
51
+ end
52
+
53
+ def mocha_with_unfulfilled_expectation
54
+ mockee = mock()
55
+ mockee.expects(:blah)
56
+ end
57
+
58
+ def mocha_with_unexpected_invocation
59
+ mockee = mock()
60
+ mockee.blah
61
+ end
62
+
63
+ def stubba_with_fulfilled_expectation
64
+ stubbee = Class.new { define_method(:blah) {} }.new
65
+ stubbee.expects(:blah)
66
+ stubbee.blah
67
+ end
68
+
69
+ def stubba_with_unfulfilled_expectation
70
+ stubbee = Class.new { define_method(:blah) {} }.new
71
+ stubbee.expects(:blah)
72
+ end
73
+
74
+ def mocha_with_matching_parameter
75
+ mockee = mock()
76
+ mockee.expects(:blah).with(has_key(:wibble))
77
+ mockee.blah(:wibble => 1)
78
+ end
79
+
80
+ def mocha_with_non_matching_parameter
81
+ mockee = mock()
82
+ mockee.expects(:blah).with(has_key(:wibble))
83
+ mockee.blah(:wobble => 2)
84
+ end
85
+
86
+ end
87
+
88
+ require 'test/unit'
89
+
90
+ class APITest < Test::Unit::TestCase
91
+
92
+ attr_reader :sample_test
93
+
94
+ include AcceptanceTest
95
+
96
+ def setup
97
+ @sample_test = SampleTest.new
98
+ setup_acceptance_test
99
+ end
100
+
101
+ def teardown
102
+ teardown_acceptance_test
103
+ end
104
+
105
+ def test_should_pass_mocha_test
106
+ assert_nothing_raised { sample_test.run(:mocha_with_fulfilled_expectation) }
107
+ assert_equal 1, sample_test.assertion_counter.count
108
+ end
109
+
110
+ def test_should_fail_mocha_test_due_to_unfulfilled_exception
111
+ assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unfulfilled_expectation) }
112
+ assert_equal 1, sample_test.assertion_counter.count
113
+ end
114
+
115
+ def test_should_fail_mocha_test_due_to_unexpected_invocation
116
+ assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unexpected_invocation) }
117
+ assert_equal 0, sample_test.assertion_counter.count
118
+ end
119
+
120
+ def test_should_pass_stubba_test
121
+ assert_nothing_raised { sample_test.run(:stubba_with_fulfilled_expectation) }
122
+ assert_equal 1, sample_test.assertion_counter.count
123
+ end
124
+
125
+ def test_should_fail_stubba_test
126
+ assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:stubba_with_unfulfilled_expectation) }
127
+ assert_equal 1, sample_test.assertion_counter.count
128
+ end
129
+
130
+ def test_should_pass_mocha_test_with_matching_parameter
131
+ assert_nothing_raised { sample_test.run(:mocha_with_matching_parameter) }
132
+ assert_equal 1, sample_test.assertion_counter.count
133
+ end
134
+
135
+ def test_should_fail_mocha_test_with_non_matching_parameter
136
+ assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_non_matching_parameter) }
137
+ end
138
+
139
+ end
@@ -0,0 +1,67 @@
1
+ # This is not meant to be run by itself. It will be run by bacon_test.rb
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib"))
3
+ require 'bacon'
4
+ require 'mocha'
5
+
6
+ module MetaTestOutput
7
+ def handle_specification(name)
8
+ yield
9
+ end
10
+
11
+ def handle_requirement(description)
12
+ yield
13
+ end
14
+
15
+ def handle_summary
16
+ puts
17
+ puts Bacon::ErrorLog if Bacon::Backtraces
18
+ puts "%d tests, %d assertions, %d failures, %d errors" %
19
+ Bacon::Counter.values_at(:specifications, :requirements, :failed, :errors)
20
+ end
21
+
22
+ end
23
+
24
+ Bacon.extend MetaTestOutput
25
+ Bacon.summary_on_exit
26
+
27
+ describe "Bacon specs using Mocha" do
28
+ should "pass when all expectations were fulfilled" do
29
+ mockee = mock()
30
+ mockee.expects(:blah)
31
+ mockee.blah
32
+ end
33
+
34
+ should "fail when not all expectations were fulfilled" do
35
+ mockee = mock()
36
+ mockee.expects(:blah)
37
+ end
38
+
39
+ should "fail when there is an unexpected invocation" do
40
+ mockee = mock()
41
+ mockee.blah
42
+ end
43
+
44
+ should "pass when they receive all expected parameters" do
45
+ mockee = mock()
46
+ mockee.expects(:blah).with(has_key(:wibble))
47
+ mockee.blah(:wibble => 1)
48
+ end
49
+
50
+ should "fail when they receive unexpected parameters" do
51
+ mockee = mock()
52
+ mockee.expects(:blah).with(has_key(:wibble))
53
+ mockee.blah(:wobble => 2)
54
+ end
55
+
56
+ should "pass when all Stubba expectations are fulfilled" do
57
+ stubbee = Class.new { define_method(:blah) {} }.new
58
+ stubbee.expects(:blah)
59
+ stubbee.blah
60
+ end
61
+
62
+ should "fail when not all Stubba expectations were fulfilled" do
63
+ stubbee = Class.new { define_method(:blah) {} }.new
64
+ stubbee.expects(:blah)
65
+ end
66
+
67
+ end