mocha 0.1

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 (43) hide show
  1. data/COPYING +3 -0
  2. data/README +148 -0
  3. data/lib/auto_mocha.rb +1 -0
  4. data/lib/auto_mocha/auto_mock.rb +54 -0
  5. data/lib/auto_mocha/mock_class.rb +38 -0
  6. data/lib/mocha.rb +1 -0
  7. data/lib/mocha/expectation.rb +116 -0
  8. data/lib/mocha/infinite_range.rb +27 -0
  9. data/lib/mocha/inspect.rb +37 -0
  10. data/lib/mocha/metaclass.rb +7 -0
  11. data/lib/mocha/mock.rb +30 -0
  12. data/lib/mocha/mock_methods.rb +55 -0
  13. data/lib/mocha/pretty_parameters.rb +28 -0
  14. data/lib/stubba.rb +2 -0
  15. data/lib/stubba/any_instance_method.rb +31 -0
  16. data/lib/stubba/class_method.rb +61 -0
  17. data/lib/stubba/instance_method.rb +22 -0
  18. data/lib/stubba/object.rb +77 -0
  19. data/lib/stubba/stubba.rb +27 -0
  20. data/lib/stubba/test_case.rb +65 -0
  21. data/test/all_tests.rb +100 -0
  22. data/test/auto_mocha/auto_mock_test.rb +85 -0
  23. data/test/auto_mocha/mock_class_test.rb +179 -0
  24. data/test/auto_mock_acceptance_test.rb +36 -0
  25. data/test/method_definer.rb +18 -0
  26. data/test/mocha/expectation_test.rb +216 -0
  27. data/test/mocha/infinite_range_test.rb +50 -0
  28. data/test/mocha/inspect_test.rb +79 -0
  29. data/test/mocha/mock_methods_test.rb +141 -0
  30. data/test/mocha/mock_test.rb +64 -0
  31. data/test/mocha/pretty_parameters_test.rb +32 -0
  32. data/test/mocha_acceptance_test.rb +112 -0
  33. data/test/stubba/any_instance_method_test.rb +113 -0
  34. data/test/stubba/class_method_test.rb +149 -0
  35. data/test/stubba/instance_method_test.rb +97 -0
  36. data/test/stubba/object_test.rb +147 -0
  37. data/test/stubba/stubba_test.rb +62 -0
  38. data/test/stubba/test_case_test.rb +41 -0
  39. data/test/stubba_acceptance_test.rb +107 -0
  40. data/test/stubba_integration_test.rb +59 -0
  41. data/test/stubba_replacer.rb +13 -0
  42. data/test/test_helper.rb +4 -0
  43. metadata +91 -0
@@ -0,0 +1,85 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'auto_mocha/auto_mock'
3
+
4
+ class AutoMockTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ reset_mochas
8
+ end
9
+
10
+ def test_should_reset_mochas
11
+ undefined_class = UndefinedClass
12
+ reset_mochas
13
+ assert_not_same undefined_class, UndefinedClass
14
+ end
15
+
16
+ def test_should_refer_to_same_class
17
+ defined_class = Class.new { def self.undefined_class; UndefinedClass; end }
18
+ assert_same UndefinedClass, defined_class.undefined_class
19
+ end
20
+
21
+ def test_should_refer_to_same_parent_class
22
+ defined_class = Class.new(UndefinedClass)
23
+ assert_same UndefinedClass, defined_class.superclass
24
+ end
25
+
26
+ def test_should_refer_to_same_namespaced_class
27
+ defined_class = Class.new { def self.namespaced_class; Namespace::UndefinedClass; end }
28
+ assert_same Namespace::UndefinedClass, defined_class.namespaced_class
29
+ end
30
+
31
+ def test_should_refer_to_different_namespaced_classes_within_test
32
+ assert ! NamespaceOne::UndefinedClass.equal?(NamespaceTwo::UndefinedClass)
33
+ end
34
+
35
+ def test_should_refer_to_different_namespaced_classes_within_class_under_test
36
+ defined_class = Class.new {
37
+ def self.class_in_namespace_one; NamespaceOne::UndefinedClass; end
38
+ def self.class_in_namespace_two; NamespaceTwo::UndefinedClass; end
39
+ }
40
+ assert ! defined_class.class_in_namespace_one.equal?(defined_class.class_in_namespace_two)
41
+ end
42
+
43
+ def test_should_refer_to_same_class_within_instance_under_test
44
+ defined_class = Class.new(UndefinedClassOne) { def undefined_class_two; UndefinedClassTwo; end }
45
+ instance = defined_class.new
46
+ assert_same UndefinedClassTwo, instance.undefined_class_two
47
+ end
48
+
49
+ def test_should_verify_all_root_mochas_when_second_method_is_not_called
50
+ ClassOne.expects(:first)
51
+ ClassTwo.expects(:second)
52
+ ClassOne.first
53
+ assert_raise(Test::Unit::AssertionFailedError) {
54
+ verify_all
55
+ }
56
+ end
57
+
58
+ def test_should_verify_all_root_mochas_when_first_method_is_not_called
59
+ ClassOne.expects(:first)
60
+ ClassTwo.expects(:second)
61
+ ClassTwo.second
62
+ assert_raise(Test::Unit::AssertionFailedError) {
63
+ verify_all
64
+ }
65
+ end
66
+
67
+ def test_should_verify_all_namespaced_mochas_when_second_method_is_not_called
68
+ NamespaceOne::ClassOne.expects(:first)
69
+ NamespaceTwo::ClassTwo.expects(:second)
70
+ NamespaceOne::ClassOne.first
71
+ assert_raise(Test::Unit::AssertionFailedError) {
72
+ verify_all
73
+ }
74
+ end
75
+
76
+ def test_should_verify_all_namespaced_mochas_when_first_method_is_not_called
77
+ NamespaceOne::ClassOne.expects(:first)
78
+ NamespaceTwo::ClassTwo.expects(:second)
79
+ NamespaceTwo::ClassTwo.second
80
+ assert_raise(Test::Unit::AssertionFailedError) {
81
+ verify_all
82
+ }
83
+ end
84
+
85
+ end
@@ -0,0 +1,179 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'auto_mocha/mock_class'
3
+
4
+ class MockClassTest < Test::Unit::TestCase
5
+
6
+ include Mocha
7
+
8
+ def test_should_not_expect_unexpected_class_method_call
9
+ klass = MockClass.dup
10
+ assert_raise(Test::Unit::AssertionFailedError) {
11
+ klass.unexpected_class_method
12
+ }
13
+ end
14
+
15
+ def test_should_expect_expected_class_method_call
16
+ klass = MockClass.dup
17
+ klass.expects(:expected_class_method)
18
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
19
+ klass.expected_class_method
20
+ }
21
+ end
22
+
23
+ def test_should_fail_verification_for_missing_class_method_call
24
+ klass = MockClass.dup
25
+ klass.expects(:expected_class_method)
26
+ assert_raise(Test::Unit::AssertionFailedError) {
27
+ klass.verify
28
+ }
29
+ end
30
+
31
+ def test_should_verify_expected_class_method_call
32
+ klass = MockClass.dup
33
+ klass.expects(:expected_class_method)
34
+ klass.expected_class_method
35
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
36
+ klass.verify
37
+ }
38
+ end
39
+
40
+ def test_should_not_expect_unexpected_child_class_method_call
41
+ parent_class = MockClass.dup
42
+ child_class = Class.new(parent_class)
43
+ assert_raise(Test::Unit::AssertionFailedError) {
44
+ child_class.unexpected_child_class_method
45
+ }
46
+ end
47
+
48
+ def test_should_expect_child_class_method_call
49
+ parent_class = MockClass.dup
50
+ child_class = Class.new(parent_class)
51
+ child_class.expects(:expected_child_class_method)
52
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
53
+ child_class.expected_child_class_method
54
+ }
55
+ end
56
+
57
+ def test_should_fail_verification_for_missing_child_class_method_call
58
+ parent_class = MockClass.dup
59
+ child_class = Class.new(parent_class)
60
+ child_class.expects(:expected_child_class_method)
61
+ assert_raise(Test::Unit::AssertionFailedError) {
62
+ child_class.verify
63
+ }
64
+ end
65
+
66
+ def test_should_verify_expected_child_class_method_call
67
+ parent_class = MockClass.dup
68
+ child_class = Class.new(parent_class)
69
+ child_class.expects(:expected_child_class_method)
70
+ child_class.expected_child_class_method
71
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
72
+ child_class.verify
73
+ }
74
+ end
75
+
76
+ def test_should_not_expect_unexpected_parent_class_method_call
77
+ parent_class = MockClass.dup
78
+ child_class = Class.new(parent_class)
79
+ assert_raise(Test::Unit::AssertionFailedError) {
80
+ child_class.unexpected_parent_class_method
81
+ }
82
+ end
83
+
84
+ def test_should_expect_parent_class_method_call
85
+ parent_class = MockClass.dup
86
+ child_class = Class.new(parent_class)
87
+ parent_class.expects(:expected_parent_class_method)
88
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
89
+ child_class.expected_parent_class_method
90
+ }
91
+ end
92
+
93
+ def test_should_fail_verification_for_missing_parent_class_method
94
+ parent_class = MockClass.dup
95
+ child_class = Class.new(parent_class)
96
+ parent_class.expects(:expected_parent_class_method)
97
+ assert_raise(Test::Unit::AssertionFailedError) {
98
+ parent_class.verify
99
+ }
100
+ end
101
+
102
+ def test_should_verify_expected_parent_class_method_call_from_child_class
103
+ parent_class = MockClass.dup
104
+ child_class = Class.new(parent_class)
105
+ parent_class.expects(:expected_parent_class_method)
106
+ child_class.expected_parent_class_method
107
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
108
+ parent_class.verify
109
+ }
110
+ end
111
+
112
+ def test_should_have_different_expectations_for_different_descendant_classes
113
+ klass1 = MockClass.dup
114
+ klass2 = MockClass.dup
115
+ klass2.expects(:my_class_method)
116
+ assert_raise(Test::Unit::AssertionFailedError) {
117
+ klass1.my_class_method
118
+ }
119
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
120
+ klass2.my_class_method
121
+ }
122
+ end
123
+
124
+ def test_should_allow_mocking_of_class_constructor
125
+ klass = MockClass.dup
126
+ expected_instance = Object.new
127
+ klass.expects(:new).returns(expected_instance)
128
+ assert_same expected_instance, klass.new
129
+ end
130
+
131
+ def test_should_use_original_constructor_for_derived_classes
132
+ parent_class = MockClass.dup
133
+ child_class = Class.new(parent_class) { attr_reader :p1, :p2; def initialize(p1, p2); @p1, @p2 = p1, p2; end }
134
+ child_instance = child_class.new(1, 2)
135
+ assert_equal 1, child_instance.p1
136
+ assert_equal 2, child_instance.p2
137
+ end
138
+
139
+ def test_should_not_expect_unexpected_parent_instance_method_call
140
+ parent_class = MockClass.dup
141
+ child_class = Class.new(parent_class)
142
+ child_instance = child_class.new
143
+ assert_raise(Test::Unit::AssertionFailedError) {
144
+ child_instance.unexpected_parent_instance_method
145
+ }
146
+ end
147
+
148
+ def test_should_expect_expected_parent_instance_method_call
149
+ parent_class = MockClass.dup
150
+ child_class = Class.new(parent_class)
151
+ child_instance = child_class.new
152
+ child_instance.expects(:expected_parent_instance_method)
153
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
154
+ child_instance.expected_parent_instance_method
155
+ }
156
+ end
157
+
158
+ def test_should_fail_verification_for_missing_parent_instance_method
159
+ parent_class = MockClass.dup
160
+ child_class = Class.new(parent_class)
161
+ child_instance = child_class.new
162
+ child_instance.expects(:expected_parent_instance_method)
163
+ assert_raise(Test::Unit::AssertionFailedError) {
164
+ child_instance.verify
165
+ }
166
+ end
167
+
168
+ def test_should_verify_expected_parent_class_method_call_from_child_class
169
+ parent_class = MockClass.dup
170
+ child_class = Class.new(parent_class)
171
+ child_instance = child_class.new
172
+ child_instance.expects(:expected_parent_instance_method)
173
+ child_instance.expected_parent_instance_method
174
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
175
+ child_instance.verify
176
+ }
177
+ end
178
+
179
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+ require 'mocha'
3
+ require 'auto_mocha'
4
+
5
+ class Product < ActiveRecord::Base
6
+
7
+ def self.released_since(cutoff_date)
8
+ find(:all, :conditions => ['release_date > :date', {:date => cutoff_date}])
9
+ end
10
+
11
+ def sales_with_price_exceeding(price_threshold)
12
+ sales.select { |sale| sale.price > price_threshold }
13
+ end
14
+
15
+ end
16
+
17
+ class AutoMockAcceptanceTest < Test::Unit::TestCase
18
+
19
+ def test_should_find_all_products_released_since_specified_date
20
+ new_releases = [Product.new]
21
+ Product.expects(:find).with(:all, :conditions => ['release_date > :date', {:date => :cutoff_date}]).returns(new_releases)
22
+ assert_equal new_releases, Product.released_since(:cutoff_date)
23
+ end
24
+
25
+ def test_should_find_sales_for_this_product_which_exceeded_specified_price
26
+ product = Product.new
27
+ cheap_order = Mocha::Mock.new
28
+ cheap_order.expects(:price).returns(5)
29
+ expensive_order = Mocha::Mock.new
30
+ expensive_order.expects(:price).returns(15)
31
+ orders = [cheap_order, expensive_order]
32
+ product.expects(:sales).returns(orders)
33
+ assert_equal [expensive_order], product.sales_with_price_exceeding(10)
34
+ end
35
+
36
+ end
@@ -0,0 +1,18 @@
1
+ require 'mocha/metaclass'
2
+
3
+ class Object
4
+
5
+ def define_instance_method(method_symbol, &block)
6
+ metaclass.send(:define_method, method_symbol, block)
7
+ end
8
+
9
+ def replace_instance_method(method_symbol, &block)
10
+ raise "Cannot replace #{method_symbol} as #{self} does not respond to it." unless self.respond_to?(method_symbol)
11
+ define_instance_method(method_symbol, &block)
12
+ end
13
+
14
+ def define_instance_accessor(*symbols)
15
+ symbols.each { |symbol| metaclass.send(:attr_accessor, symbol) }
16
+ end
17
+
18
+ end
@@ -0,0 +1,216 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'method_definer'
3
+
4
+ require 'mocha/expectation'
5
+
6
+ class ExpectationTest < Test::Unit::TestCase
7
+
8
+ include Mocha
9
+
10
+ def test_should_match_calls_to_same_method_with_any_parameters
11
+ expectation = Expectation.new(:expected_method)
12
+ assert expectation.match?(:expected_method, 1, 2, 3)
13
+ end
14
+
15
+ def test_should_match_calls_to_same_method_with_exactly_zero_parameters
16
+ expectation = Expectation.new(:expected_method).with()
17
+ assert expectation.match?(:expected_method)
18
+ end
19
+
20
+ def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
21
+ expectation = Expectation.new(:expected_method).with()
22
+ assert !expectation.match?(:expected_method, 1, 2, 3)
23
+ end
24
+
25
+ def test_should_match_calls_to_same_method_with_expected_parameter_values
26
+ expectation = Expectation.new(:expected_method).with(1, 2, 3)
27
+ assert expectation.match?(:expected_method, 1, 2, 3)
28
+ end
29
+
30
+ def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
31
+ expectation = Expectation.new(:expected_method).with() {|x, y, z| x + y == z}
32
+ assert expectation.match?(:expected_method, 1, 2, 3)
33
+ end
34
+
35
+ def test_should_not_match_calls_to_different_methods_with_no_parameters
36
+ expectation = Expectation.new(:expected_method)
37
+ assert !expectation.match?(:unexpected_method)
38
+ end
39
+
40
+ def test_should_not_match_calls_to_same_method_with_too_few_parameters
41
+ expectation = Expectation.new(:expected_method).with(1, 2, 3)
42
+ assert !expectation.match?(:unexpected_method, 1, 2)
43
+ end
44
+
45
+ def test_should_not_match_calls_to_same_method_with_too_many_parameters
46
+ expectation = Expectation.new(:expected_method).with(1, 2)
47
+ assert !expectation.match?(:unexpected_method, 1, 2, 3)
48
+ end
49
+
50
+ def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
51
+ expectation = Expectation.new(:expected_method).with(1, 2, 3)
52
+ assert !expectation.match?(:unexpected_method, 1, 0, 3)
53
+ end
54
+
55
+ def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
56
+ expectation = Expectation.new(:expected_method).with() {|x, y, z| x + y == z}
57
+ assert !expectation.match?(:expected_method, 1, 0, 3)
58
+ end
59
+
60
+ def test_should_return_specified_value
61
+ expectation = Expectation.new(:expected_method).returns(99)
62
+ assert_equal 99, expectation.invoke
63
+ end
64
+
65
+ def test_should_return_nil_if_no_value_specified
66
+ expectation = Expectation.new(:expected_method)
67
+ assert_nil expectation.invoke
68
+ end
69
+
70
+ def test_should_return_evaluated_proc
71
+ expectation = Expectation.new(:expected_method).returns(lambda { 99 })
72
+ assert_equal 99, expectation.invoke
73
+ end
74
+
75
+ def test_should_raise_runtime_exception
76
+ expectation = Expectation.new(:expected_method).raises
77
+ assert_raise(RuntimeError) { expectation.invoke }
78
+ end
79
+
80
+ def test_should_raise_custom_exception
81
+ exception = Class.new(Exception)
82
+ expectation = Expectation.new(:expected_method).raises(exception)
83
+ assert_raise(exception) { expectation.invoke }
84
+ end
85
+
86
+ def test_should_use_the_default_exception_message
87
+ expectation = Expectation.new(:expected_method).raises(Exception)
88
+ exception = assert_raise(Exception) { expectation.invoke }
89
+ assert_equal Exception.new.message, exception.message
90
+ end
91
+
92
+ def test_should_raise_custom_exception_with_message
93
+ exception_msg = "exception message"
94
+ expectation = Expectation.new(:expected_method).raises(Exception, exception_msg)
95
+ exception = assert_raise(Exception) { expectation.invoke }
96
+ assert_equal exception_msg, exception.message
97
+ end
98
+
99
+ def test_should_not_raise_error_on_verify_if_expected_call_was_made
100
+ expectation = Expectation.new(:expected_method)
101
+ expectation.invoke
102
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
103
+ expectation.verify
104
+ }
105
+ end
106
+
107
+ def test_should_not_raise_error_on_verify_if_expected_call_was_made_at_least_once
108
+ expectation = Expectation.new(:expected_method).at_least_once
109
+ 3.times {expectation.invoke}
110
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
111
+ expectation.verify
112
+ }
113
+ end
114
+
115
+ def test_should_raise_error_on_verify_if_expected_call_was_not_made_at_least_once
116
+ expectation = Expectation.new(:expected_method).with(1, 2, 3).at_least_once
117
+ e = assert_raise(Test::Unit::AssertionFailedError) {
118
+ expectation.verify
119
+ }
120
+ assert_match(/expected calls: at least 1, actual calls: 0/i, e.message)
121
+ end
122
+
123
+ def test_should_not_raise_error_on_verify_if_expected_call_was_made_expected_number_of_times
124
+ expectation = Expectation.new(:expected_method).times(2)
125
+ 2.times {expectation.invoke}
126
+ assert_nothing_raised(Test::Unit::AssertionFailedError) {
127
+ expectation.verify
128
+ }
129
+ end
130
+
131
+ def test_should_expect_call_not_to_be_made
132
+ expectation = Expectation.new(:expected_method)
133
+ expectation.define_instance_accessor(:how_many_times)
134
+ expectation.replace_instance_method(:times) { |how_many_times| self.how_many_times = how_many_times }
135
+ expectation.never
136
+ assert_equal 0, expectation.how_many_times
137
+ end
138
+
139
+ def test_should_raise_error_on_verify_if_expected_call_was_made_too_few_times
140
+ expectation = Expectation.new(:expected_method).times(2)
141
+ 1.times {expectation.invoke}
142
+ e = assert_raise(Test::Unit::AssertionFailedError) {
143
+ expectation.verify
144
+ }
145
+ assert_match(/expected calls: 2, actual calls: 1/i, e.message)
146
+ end
147
+
148
+ def test_should_raise_error_on_verify_if_expected_call_was_made_too_many_times
149
+ expectation = Expectation.new(:expected_method).times(2)
150
+ 3.times {expectation.invoke}
151
+ assert_raise(Test::Unit::AssertionFailedError) {
152
+ expectation.verify
153
+ }
154
+ end
155
+
156
+ def test_should_display_expectation_message_in_exception_message
157
+ options = [:a, :b, {:c => 1, :d => 2}]
158
+ expectation = Expectation.new(:expected_method).with(*options)
159
+ exception = assert_raise(Test::Unit::AssertionFailedError) {
160
+ expectation.verify
161
+ }
162
+ assert exception.message.include?(expectation.message)
163
+ end
164
+
165
+ def test_should_combine_method_name_and_pretty_parameters
166
+ arguments = 1, 2, {'a' => true, :b => false}, [1, 2, 3]
167
+ expectation = Expectation.new(:meth).with(*arguments)
168
+ assert_equal ":meth(#{PrettyParameters.new(arguments).pretty})", expectation.message
169
+ end
170
+
171
+ def test_should_not_include_parameters_in_message
172
+ expectation = Expectation.new(:meth)
173
+ assert_equal ":meth('** any **')", expectation.message
174
+ end
175
+
176
+ def test_should_always_verify_successfully
177
+ stub = Stub.new(:meth)
178
+ assert stub.verify
179
+ stub.invoke
180
+ assert stub.verify
181
+ end
182
+
183
+ end
184
+
185
+ class ExpectationSimilarExpectationsTest < Test::Unit::TestCase
186
+
187
+ include Mocha
188
+
189
+ attr_reader :expectation
190
+ def setup
191
+ @expectation = Expectation.new(:meth).with(2)
192
+ end
193
+
194
+ def test_should_find_expectations_to_the_same_method
195
+ failed_expectation = MissingExpectation.new(:meth, [expectation]).with(1)
196
+ assert_equal [expectation], failed_expectation.similar_expectations
197
+ end
198
+
199
+ def test_should_report_similar_expectations
200
+ missing_expectation = MissingExpectation.new(:meth, [expectation]).with(1)
201
+ exception = assert_raise(Test::Unit::AssertionFailedError) { missing_expectation.verify }
202
+ assert_equal "Unexpected message :meth(1)\nSimilar expectations :meth(2)", exception.message
203
+ end
204
+
205
+ def test_should_ignore_expectations_to_different_methods
206
+ failed_expectation = MissingExpectation.new(:other_meth, [expectation]).with(1)
207
+ assert failed_expectation.similar_expectations.empty?
208
+ end
209
+
210
+ def test_should_not_report_similar_expectations
211
+ missing_expectation = MissingExpectation.new(:other_meth, [expectation]).with(1)
212
+ exception = assert_raise(Test::Unit::AssertionFailedError) { missing_expectation.verify }
213
+ assert_equal "Unexpected message :other_meth(1)", exception.message
214
+ end
215
+
216
+ end