flexmock 1.3.3 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.autotest +3 -0
  3. data/.gitignore +14 -0
  4. data/.togglerc +7 -0
  5. data/.travis.yml +5 -0
  6. data/.yardopts +2 -0
  7. data/CHANGES +11 -0
  8. data/Gemfile +1 -4
  9. data/README.md +39 -11
  10. data/Rakefile +6 -217
  11. data/doc/examples/rspec_examples_spec.rb +244 -0
  12. data/doc/examples/test_unit_examples_test.rb +240 -0
  13. data/doc/jamis.rb +591 -0
  14. data/flexmock.gemspec +33 -0
  15. data/lib/flexmock.rb +0 -1
  16. data/lib/flexmock/composite_expectation.rb +1 -1
  17. data/lib/flexmock/core.rb +3 -7
  18. data/lib/flexmock/core_class_methods.rb +5 -1
  19. data/lib/flexmock/default_framework_adapter.rb +2 -2
  20. data/lib/flexmock/expectation.rb +29 -3
  21. data/lib/flexmock/expectation_director.rb +1 -1
  22. data/lib/flexmock/minitest.rb +13 -0
  23. data/lib/flexmock/minitest_extensions.rb +26 -0
  24. data/lib/flexmock/minitest_integration.rb +111 -0
  25. data/lib/flexmock/mock_container.rb +1 -2
  26. data/lib/flexmock/partial_mock.rb +61 -104
  27. data/lib/flexmock/recorder.rb +1 -2
  28. data/lib/flexmock/rspec.rb +6 -3
  29. data/lib/flexmock/test_unit_integration.rb +14 -0
  30. data/lib/flexmock/validators.rb +5 -4
  31. data/lib/flexmock/version.rb +1 -9
  32. data/rakelib/metrics.rake +40 -0
  33. data/rakelib/preview.rake +4 -0
  34. data/rakelib/tags.rake +18 -0
  35. data/todo.txt +20 -0
  36. metadata +61 -86
  37. data/Gemfile.lock +0 -20
  38. data/doc/examples/rspec_examples_spec.rdoc +0 -245
  39. data/doc/examples/test_unit_examples_test.rdoc +0 -241
  40. data/test/aliasing_test.rb +0 -66
  41. data/test/assert_spy_called_test.rb +0 -119
  42. data/test/base_class_test.rb +0 -71
  43. data/test/based_partials_test.rb +0 -51
  44. data/test/container_methods_test.rb +0 -118
  45. data/test/default_framework_adapter_test.rb +0 -38
  46. data/test/demeter_mocking_test.rb +0 -191
  47. data/test/deprecated_methods_test.rb +0 -225
  48. data/test/examples_from_readme_test.rb +0 -157
  49. data/test/expectation_description_test.rb +0 -80
  50. data/test/extended_should_receive_test.rb +0 -69
  51. data/test/flexmodel_test.rb +0 -54
  52. data/test/mock_builder_test.rb +0 -68
  53. data/test/naming_test.rb +0 -84
  54. data/test/new_instances_test.rb +0 -215
  55. data/test/object_extensions_test.rb +0 -25
  56. data/test/partial_mock_test.rb +0 -458
  57. data/test/record_mode_test.rb +0 -158
  58. data/test/redirect_error.rb +0 -16
  59. data/test/rspec_integration/integration_spec.rb +0 -56
  60. data/test/rspec_integration/spy_example_spec.rb +0 -207
  61. data/test/samples_test.rb +0 -283
  62. data/test/should_ignore_missing_test.rb +0 -84
  63. data/test/should_receive_test.rb +0 -1155
  64. data/test/spys_test.rb +0 -215
  65. data/test/symbol_extensions_test.rb +0 -8
  66. data/test/test_class_extensions.rb +0 -34
  67. data/test/test_setup.rb +0 -92
  68. data/test/test_unit_integration/auto_test_unit_test.rb +0 -42
  69. data/test/test_unit_integration/minitest_teardown_test.rb +0 -14
  70. data/test/tu_integration_test.rb +0 -99
  71. data/test/undefined_test.rb +0 -87
@@ -1,241 +0,0 @@
1
- = FlexMock Examples
2
- require 'flexmock/test_unit'
3
-
4
- class TestSimple < Test::Unit::TestCase
5
-
6
- # Simple stubbing of some methods
7
-
8
- def test_simple_mock
9
- m = flexmock(:pi => 3.1416, :e => 2.71)
10
- assert_equal 3.1416, m.pi
11
- assert_equal 2.71, m.e
12
- end
13
- end
14
-
15
-
16
- class TestUndefined < Test::Unit::TestCase
17
-
18
- # Create a mock object that returns an undefined object for method calls
19
-
20
- def test_undefined_values
21
- m = flexmock("mock")
22
- m.should_receive(:divide_by).with(0).
23
- and_return_undefined
24
- assert_equal FlexMock.undefined, m.divide_by(0)
25
- end
26
- end
27
-
28
- class TestDb < Test::Unit::TestCase
29
-
30
- # Expect multiple queries and a single update
31
-
32
- # Multiple calls to the query method will be allows, and calls may
33
- # have any argument list. Each call to query will return the three
34
- # element array [1, 2, 3]. The call to update must have a specific
35
- # argument of 5.
36
-
37
- def test_db
38
- db = flexmock('db')
39
- db.should_receive(:query).and_return([1,2,3])
40
- db.should_receive(:update).with(5).and_return(nil).once
41
-
42
- # Test Code
43
-
44
- db.query
45
- db.update(5)
46
- end
47
- end
48
-
49
- class TestOrdered < Test::Unit::TestCase
50
-
51
- # Expect all queries before any updates
52
-
53
- # All the query message must occur before any of the update
54
- # messages.
55
-
56
- def test_query_and_update
57
- db = flexmock('db')
58
- db.should_receive(:query).and_return([1,2,3]).ordered
59
- db.should_receive(:update).and_return(nil).ordered
60
-
61
- # test code here
62
-
63
- db.query
64
- db.update
65
- end
66
- end
67
-
68
- class MoreOrdered < Test::Unit::TestCase
69
-
70
- # Expect several queries with different parameters
71
-
72
- # The queries should happen after startup but before finish. The
73
- # queries themselves may happen in any order (because they are in
74
- # the same order group). The first two queries should happen exactly
75
- # once, but the third query (which matches any query call with a
76
- # four character parameter) may be called multiple times (but at
77
- # least once). Startup and finish must also happen exactly once.
78
-
79
- # Also note that we use the <code>with</code> method to match
80
- # different argument values to figure out what value to return.
81
-
82
- def test_ordered_queries
83
- db = flexmock('db')
84
- db.should_receive(:startup).once.ordered
85
- db.should_receive(:query).with("CPWR").and_return(12.3).
86
- once.ordered(:queries)
87
- db.should_receive(:query).with("MSFT").and_return(10.0).
88
- once.ordered(:queries)
89
- db.should_receive(:query).with(/^....$/).and_return(3.3).
90
- at_least.once.ordered(:queries)
91
- db.should_receive(:finish).once.ordered
92
-
93
- # Test Code
94
-
95
- db.startup
96
- db.query("MSFT")
97
- db.query("XYZY")
98
- db.query("CPWR")
99
- db.finish
100
- end
101
- end
102
-
103
- class EvenMoreOrderedTest < Test::Unit::TestCase
104
-
105
- # Same as above, but using the Record Mode interface
106
-
107
- # The record mode interface offers much the same features as the
108
- # <code>should_receive</code> interface introduced so far, but it
109
- # allows the messages to be sent directly to a recording object
110
- # rather than be specified indirectly using a symbol.
111
-
112
-
113
- def test_ordered_queries_in_record_mode
114
- db = flexmock('db')
115
- db.should_expect do |rec|
116
- rec.startup.once.ordered
117
- rec.query("CPWR") { 12.3 }.once.ordered(:queries)
118
- rec.query("MSFT") { 10.0 }.once.ordered(:queries)
119
- rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
120
- rec.finish.once.ordered
121
- end
122
-
123
- # Test Code
124
-
125
- db.startup
126
- db.query("MSFT")
127
- db.query("XYZY")
128
- db.query("CPWR")
129
- db.finish
130
- end
131
- end
132
-
133
- class RecordedTest < Test::Unit::TestCase
134
-
135
- # Using Record Mode to record a known, good algorithm for testing
136
-
137
- # Record mode is nice when you have a known, good algorithm that can
138
- # use a recording mock object to record the steps. Then you compare
139
- # the execution of a new algorithm to behavior of the old using the
140
- # recorded expectations in the mock. For this you probably want to
141
- # put the recorder in _strict_ mode so that the recorded
142
- # expectations use exact matching on argument lists, and strict
143
- # ordering of the method calls.
144
-
145
- # <b>Note:</b> This is most useful when there are no queries on the
146
- # mock objects, because the query responses cannot be programmed
147
- # into the recorder object.
148
-
149
- def test_build_xml
150
- builder = flexmock('builder')
151
- builder.should_expect do |rec|
152
- rec.should_be_strict
153
- known_good_way_to_build_xml(rec) # record the messages
154
- end
155
- new_way_to_build_xml(builder) # compare to new way
156
- end
157
-
158
- def known_good_way_to_build_xml(builder)
159
- builder.person
160
- end
161
-
162
- def new_way_to_build_xml(builder)
163
- builder.person
164
- end
165
-
166
- end
167
-
168
- class MultipleReturnValueTest < Test::Unit::TestCase
169
-
170
- # Expect multiple calls, returning a different value each time
171
-
172
- # Sometimes you need to return different values for each call to a
173
- # mocked method. This example shifts values out of a list for this
174
- # effect.
175
-
176
- def test_multiple_gets
177
- file = flexmock('file')
178
- file.should_receive(:gets).with_no_args.
179
- and_return("line 1\n", "line 2\n")
180
-
181
- # test code here
182
-
183
- file.gets # returns "line 1"
184
- file.gets # returns "line 2"
185
- end
186
- end
187
-
188
- class IgnoreUnimportantMessages < Test::Unit::TestCase
189
-
190
- # Ignore uninteresting messages
191
-
192
- # Generally you need to mock only those methods that return an
193
- # interesting value or wish to assert were sent in a particular
194
- # manner. Use the <code>should_ignore_missing</code> method to turn
195
- # on missing method ignoring.
196
-
197
- def test_an_important_message
198
- m = flexmock('m')
199
- m.should_receive(:an_important_message).and_return(1).once
200
- m.should_ignore_missing
201
-
202
- # Test Code
203
-
204
- m.an_important_message
205
- m.an_unimportant_message
206
- end
207
-
208
- # When <code>should_ignore_missing</code> is enabled, ignored
209
- # missing methods will return an undefined object. Any operation on
210
- # the undefined object will return the undefined object.
211
-
212
- end
213
-
214
- class PartialMockTest < Test::Unit::TestCase
215
-
216
- # Mock just one method on an existing object
217
-
218
- # The Portfolio class calculate the value of a set of stocks by
219
- # talking to a quote service via a web service. Since we don't want
220
- # to use a real web service in our unit tests, we will mock the
221
- # quote service.
222
-
223
- def test_portfolio_value
224
- flexmock(QuoteService).new_instances do |m|
225
- m.should_receive(:quote).and_return(100)
226
- end
227
- port = Portfolio.new
228
- value = port.value # Portfolio calls QuoteService.quote
229
- assert_equal 100, value
230
- end
231
-
232
- class QuoteService
233
- end
234
-
235
- class Portfolio
236
- def value
237
- qs = QuoteService.new
238
- qs.quote
239
- end
240
- end
241
- end
@@ -1,66 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/test_setup'
4
-
5
- # This is an experimental test to see if mock and stub methods can
6
- # easily be added to a flexmock core. These test are disabled by
7
- # default, and should be reviewed before something more production
8
- # oriented is provided.
9
-
10
- if ENV['TEST_ALIASES']
11
-
12
- class FlexMock
13
- module StubsAndExpects
14
- def expects(*args)
15
- result = should_receive(*args)
16
- result.at_least.once unless result.call_count_constrained?
17
- result
18
- end
19
- def stubs(*args)
20
- should_receive(*args)
21
- end
22
- end
23
-
24
- module MockContainer
25
- alias :mock :flexmock
26
- alias :stub :flexmock
27
- end
28
-
29
- include StubsAndExpects
30
-
31
- class PartialMockProxy
32
- include StubsAndExpects
33
- MOCK_METHODS << :stubs << :expects
34
- end
35
- end
36
-
37
- class AliasingTest < Test::Unit::TestCase
38
- include FlexMock::TestCase
39
-
40
- def test_mocking
41
- m = mock("a cute dog").expects(:pat).twice.and_return(:woof!).mock
42
- assert_equal :woof!, m.pat
43
- assert_equal :woof!, m.pat
44
- end
45
-
46
- def test_once_mocking
47
- mock("a cute dog").expects(:pat).and_return(:woof!).mock
48
- end
49
-
50
- def test_twice_mocking
51
- m = mock("a cute dog").expects(:pat).and_return(:woof!).twice.mock
52
- assert_raises(assertion_failed_error) { m.flexmock_verify }
53
- end
54
-
55
- def test_stubbing
56
- m = stub("a cute dog").expects(:pat).and_return(:woof!).mock
57
- assert_equal :woof!, m.pat
58
- end
59
-
60
- def test_partial
61
- obj = Object.new
62
- stub(obj).stubs(:wag).and_return(:tail)
63
- assert_equal :tail, obj.wag
64
- end
65
- end
66
- end
@@ -1,119 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/test_setup'
4
- require 'flexmock/test_unit_assert_spy_called'
5
-
6
- class AssertSpyCalledTest < Test::Unit::TestCase
7
- include FlexMock::TestCase
8
-
9
- class FooBar
10
- def foo
11
- end
12
- def bar
13
- end
14
- end
15
-
16
- def setup
17
- super
18
- @spy = flexmock(:on, FooBar)
19
- end
20
-
21
- def spy
22
- @spy
23
- end
24
-
25
- def test_assert_detects_basic_call
26
- spy.foo
27
- assert_spy_called spy, :foo
28
- end
29
-
30
- def test_assert_detects_basic_call_with_args
31
- spy.foo(1,2)
32
- assert_spy_called spy, :foo, 1, 2
33
- end
34
-
35
- def test_assert_rejects_incorrect_args
36
- spy.foo(1,2)
37
- assert_fails(/^expected foo\(1, 3\) to be received by <FlexMock:AssertSpyCalledTest::FooBar Mock>/i) do
38
- assert_spy_called spy, :foo, 1, 3
39
- end
40
- end
41
-
42
- def test_assert_detects_multiple_calls
43
- spy.foo
44
- spy.foo
45
- spy.foo
46
- assert_spy_called spy, {:times => 3}, :foo
47
- end
48
-
49
- def test_assert_rejects_incorrect_type
50
- spy.foo
51
- spy.foo
52
- assert_fails(/^expected foo\(\) to be received by <FlexMock:AssertSpyCalledTest::FooBar Mock> 3 times/i) do
53
- assert_spy_called spy, {:times => 3}, :foo
54
- end
55
- end
56
-
57
- def test_assert_detects_blocks
58
- spy.foo { }
59
- spy.bar
60
- assert_spy_called spy, :foo, Proc
61
- assert_spy_called spy, :bar
62
- end
63
-
64
- def test_assert_detects_any_args
65
- spy.foo
66
- spy.foo(1)
67
- spy.foo("HI")
68
- spy.foo("Hello", "World", 10, :options => true)
69
- assert_spy_called spy, {:times => 4}, :foo, :_
70
- end
71
-
72
- def test_assert_rejects_bad_count_on_any_args
73
- spy.foo
74
- assert_fails(/^expected foo\(\.\.\.\) to be received by <FlexMock:AssertSpyCalledTest::FooBar Mock> twice/i) do
75
- assert_spy_called spy, {:times => 2}, :foo, :_
76
- end
77
- end
78
-
79
- def test_assert_error_lists_calls_actually_made_without_handled_by
80
- spy.foo
81
- spy.bar(1)
82
- ex = assert_fails(/The following messages have been received/) do
83
- assert_spy_called spy, :baz
84
- end
85
- assert_match(/ foo\(\)/, ex.message)
86
- assert_match(/ bar\(1\)/, ex.message)
87
- assert_no_match(/ baz\(\)/, ex.message)
88
- assert_no_match(/handled by/, ex.message)
89
- end
90
-
91
- def test_assert_error_lists_calls_actually_made_with_handled_by
92
- spy.should_receive(:foo).once
93
- spy.foo
94
- spy.bar(1)
95
- ex = assert_fails(/The following messages have been received/) do
96
- assert_spy_called spy, :baz
97
- end
98
- assert_match(/ foo\(\) matched by should_receive\(:foo\)/, ex.message)
99
- assert_match(/ bar\(1\)/, ex.message)
100
- assert_no_match(/ baz\(\)/, ex.message)
101
- end
102
-
103
- def test_assert_errors_say_no_calls_made
104
- assert_fails(/No messages have been received/) do
105
- assert_spy_called spy, :baz
106
- end
107
- end
108
-
109
- private
110
-
111
- def assert_fails(message_pattern)
112
- ex = assert_raises(FlexMock.framework_adapter.assertion_failed_error) do
113
- yield
114
- end
115
- assert_match(message_pattern, ex.message)
116
- ex
117
- end
118
-
119
- end
@@ -1,71 +0,0 @@
1
- require 'test/test_setup'
2
-
3
- class BaseClassTest < Test::Unit::TestCase
4
- include FlexMock::TestCase
5
-
6
- class FooBar
7
- def foo
8
- end
9
-
10
- def method_missing(sym, *args, &block)
11
- return :poof if sym == :barq
12
- super
13
- end
14
-
15
- def respond_to?(method)
16
- method == :barq || super
17
- end
18
- end
19
-
20
- def mock
21
- @mock ||= flexmock(:on, FooBar)
22
- end
23
-
24
- def test_base_class_auto_mocks_class
25
- assert_equal FooBar, mock.class
26
- end
27
-
28
- def test_base_class_auto_mocks_base_class_methods
29
- assert_equal FlexMock.undefined, mock.foo
30
- end
31
-
32
- def test_base_class_does_not_mock_non_base_class_methods
33
- assert_raise(NoMethodError) do
34
- mock.fuzz
35
- end
36
- end
37
-
38
- def test_can_stub_existing_methods
39
- mock.should_receive(:foo => :bar)
40
- assert_equal :bar, mock.foo
41
- end
42
-
43
- def test_can_not_stub_non_class_defined_methods
44
- ex = assert_raises(NoMethodError) do
45
- mock.should_receive(:baz => :bar)
46
- end
47
- assert_match(/can *not stub methods.*base.*class/i, ex.message)
48
- assert_match(/class:.+FooBar/i, ex.message)
49
- assert_match(/method:.+baz/i, ex.message)
50
- end
51
-
52
- def test_can_not_stub_non_class_methods_in_single_line
53
- ex = assert_raises(NoMethodError) do
54
- flexmock(:on, FooBar, :bark => :value)
55
- end
56
- assert_match(/can *not stub methods.*base.*class/i, ex.message)
57
- assert_match(/class:.+FooBar/i, ex.message)
58
- assert_match(/method:.+bark/i, ex.message)
59
- end
60
-
61
- def test_can_explicitly_stub_non_class_defined_methods
62
- mock.should_receive(:baz).explicitly.and_return(:bar)
63
- assert_equal :bar, mock.baz
64
- end
65
-
66
- def test_can_explicitly_stub_meta_programmed_methods
67
- mock.should_receive(:barq).explicitly.and_return(:bar)
68
- assert_equal :bar, mock.barq
69
- end
70
-
71
- end