flexmock 0.7.1 → 0.8.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.
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #---
4
+ # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #+++
11
+
12
+ require 'test/unit'
13
+ require 'flexmock'
14
+
15
+ class TemperatureSampler
16
+ def initialize(sensor)
17
+ @sensor = sensor
18
+ end
19
+
20
+ def average_temp
21
+ total = (0...3).collect { @sensor.read_temperature }.inject { |i, s| i + s }
22
+ total / 3.0
23
+ end
24
+ end
25
+
26
+ class TestTemperatureSampler < Test::Unit::TestCase
27
+ include FlexMock::TestCase
28
+
29
+ def test_tempurature_sampler
30
+ readings = [10, 12, 14]
31
+ mock_sensor = flexmock("sensor")
32
+ mock_sensor.should_receive(:read_temperature).and_return { readings.shift }
33
+ sampler = TemperatureSampler.new(mock_sensor)
34
+ assert_equal 12, sampler.average_temp
35
+ end
36
+ end
37
+
38
+ class TestExamplesFromReadme < Test::Unit::TestCase
39
+ include FlexMock::TestCase
40
+
41
+ def test_simple_return_values
42
+ m = flexmock(:pi => 3.1416, :e => 2.71)
43
+ assert_equal 3.1416, m.pi
44
+ assert_equal 2.71, m.e
45
+ end
46
+
47
+ def test_returning_an_undefined_value
48
+ m = flexmock("mock")
49
+ m.should_receive(:foo).and_return_undefined
50
+ m.foo.bar.baz
51
+ end
52
+
53
+ def test_db
54
+ db = flexmock('db')
55
+ db.should_receive(:query).and_return([1,2,3])
56
+ db.should_receive(:update).with(5).and_return(nil).once
57
+ # test code here
58
+ assert_equal [1, 2, 3], db.query
59
+ db.update(5)
60
+ end
61
+
62
+ def test_query_and_update
63
+ db = flexmock('db')
64
+ db.should_receive(:query).and_return([1,2,3]).ordered
65
+ db.should_receive(:update).and_return(nil).ordered
66
+ # test code here
67
+ assert_equal [1,2,3], db.query
68
+ assert_nil db.update
69
+ end
70
+
71
+ def test_ordered_queries
72
+ db = flexmock('db')
73
+ db.should_receive(:startup).once.ordered
74
+ db.should_receive(:query).with("GOOG").and_return(12.3).
75
+ once.ordered(:queries)
76
+ db.should_receive(:query).with("APPL").and_return(10.0).
77
+ once.ordered(:queries)
78
+ db.should_receive(:query).with(/^....$/).and_return(3.3).
79
+ at_least.once.ordered(:queries)
80
+ db.should_receive(:finish).once.ordered
81
+ # test code here
82
+ db.startup
83
+ assert_equal 3.3, db.query("WXYZ")
84
+ assert_equal 10.0, db.query("APPL")
85
+ assert_equal 12.3, db.query("GOOG")
86
+ db.finish
87
+ end
88
+
89
+ def test_ordered_queries_in_record_mode
90
+ db = flexmock('db')
91
+ db.should_expect do |rec|
92
+ rec.startup.once.ordered
93
+ rec.query("GOOG") { 12.3 }.once.ordered(:queries)
94
+ rec.query("APPL") { 10.0 }.once.ordered(:queries)
95
+ rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
96
+ rec.finish.once.ordered
97
+ end
98
+ # test code here using +db+.
99
+ db.startup
100
+ assert_equal 10.0, db.query("APPL")
101
+ assert_equal 12.3, db.query("GOOG")
102
+ assert_equal 3.3, db.query("WXYZ")
103
+ db.finish
104
+ end
105
+
106
+ def test_build_xml
107
+ builder = flexmock('builder')
108
+ builder.should_expect do |rec|
109
+ rec.should_be_strict
110
+ known_good_way_to_build_xml(rec) # record the messages
111
+ end
112
+ new_way_to_build_xml(builder) # compare to new way
113
+ end
114
+
115
+ def known_good_way_to_build_xml(rec)
116
+ rec.one
117
+ rec.two
118
+ end
119
+
120
+ def new_way_to_build_xml(rec)
121
+ [:one, :two].each do |sym| rec.send(sym) end
122
+ end
123
+
124
+ def test_multiple_gets
125
+ file = flexmock('file')
126
+ file.should_receive(:gets).with_no_args.
127
+ and_return("line 1\n", "line 2\n")
128
+ # test code here
129
+ assert_equal "line 1\n", file.gets
130
+ assert_equal "line 2\n", file.gets
131
+ end
132
+
133
+ def test_an_important_message
134
+ m = flexmock('m')
135
+ m.should_receive(:an_important_message).and_return(1).once
136
+ m.should_ignore_missing
137
+ # test code here
138
+ m.an_important_message
139
+ m.unknown_message.bar.baz
140
+ end
141
+
142
+ class QuoteService
143
+ end
144
+ class Portfolio
145
+ def value
146
+ QuoteService.new.quote
147
+ end
148
+ end
149
+
150
+ def test_portfolio_value
151
+ flexmock(QuoteService).new_instances do |m|
152
+ m.should_receive(:quote).and_return(100)
153
+ end
154
+ port = Portfolio.new
155
+ value = port.value # Portfolio calls QuoteService.quote
156
+ assert_equal 100, value
157
+ end
158
+ end
@@ -25,6 +25,18 @@ class TestStubbing < Test::Unit::TestCase
25
25
  end
26
26
  end
27
27
 
28
+ class DogPlus < Dog
29
+ def should_receive
30
+ :dog_should
31
+ end
32
+ def new_instances
33
+ :dog_new
34
+ end
35
+ def by_default
36
+ :dog_by_default
37
+ end
38
+ end
39
+
28
40
  def test_stub_command_add_behavior_to_arbitrary_objects
29
41
  obj = Object.new
30
42
  flexmock(obj).should_receive(:hi).once.and_return(:stub_hi)
@@ -289,6 +301,13 @@ class TestStubbing < Test::Unit::TestCase
289
301
  assert_equal :mocked, dog.bark
290
302
  end
291
303
 
304
+ def test_should_receive_does_not_override_preexisting_def
305
+ dog = flexmock(DogPlus.new)
306
+ assert_equal :dog_should, dog.should_receive
307
+ assert_equal :dog_new, dog.new_instances
308
+ assert_equal :dog_by_default, dog.by_default
309
+ end
310
+
292
311
  class Liar
293
312
  def respond_to?(method_name)
294
313
  sym = method_name.to_sym
@@ -311,5 +330,28 @@ class TestStubbing < Test::Unit::TestCase
311
330
  flexmock(liar, :not_defined => :xyzzy)
312
331
  assert_equal :xyzzy, liar.not_defined
313
332
  end
333
+
334
+ # The following test was suggested by Pat Maddox for the RSpec
335
+ # mocks. Evidently the (poorly implemented) == method caused issues
336
+ # with RSpec Mock's internals. I'm just double checking for any
337
+ # similar issues in FlexMock as well.
338
+
339
+ class ValueObject
340
+ attr_reader :val
341
+
342
+ def initialize(val)
343
+ @val = val
344
+ end
345
+
346
+ def ==(other)
347
+ @val == other.val
348
+ end
349
+ end
350
+
351
+ def test_partial_mocks_in_the_presense_of_equal_definition
352
+ flexmock("existing obj", :foo => :foo)
353
+ obj = ValueObject.new(:bar)
354
+ flexmock(obj, :some_method => :some_method)
355
+ end
314
356
 
315
357
  end
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'flexmock/rails/view_mocking'
5
+
6
+ ######################################################################
7
+ class TestRailsViewStub < Test::Unit::TestCase
8
+ include FlexMock::TestCase
9
+
10
+ def setup
11
+ @controller_class = flexmock("controller class")
12
+ @controller = flexmock("controller", :class => @controller_class)
13
+ end
14
+
15
+ def test_view_mocks_as_stub
16
+ should_render_view
17
+ render "controller/new.rthml"
18
+ end
19
+
20
+ def test_fails_if_no_render
21
+ should_render_view
22
+ assert_raise(Test::Unit::AssertionFailedError) do
23
+ flexmock_verify
24
+ end
25
+ end
26
+
27
+ def test_view_mocks_with_expectation
28
+ should_render_view("new")
29
+ render "controller/new"
30
+ end
31
+
32
+ def test_view_mocks_with_expectation_fails_with_different_template
33
+ should_render_view("new")
34
+ render "controller/edit"
35
+ assert_raise(Test::Unit::AssertionFailedError) do
36
+ flexmock_verify
37
+ end
38
+ end
39
+
40
+ def test_view_mocks_with_expectation_wand_multiple_templates
41
+ should_render_view("new")
42
+ render "controller/edit", "controller/new", "controller/show"
43
+ end
44
+
45
+ private
46
+
47
+ def render(*names)
48
+ vc = @controller.class.view_class
49
+ v = vc.new
50
+ v.assigns(:x => :y)
51
+ v.render_file
52
+ v.first_render
53
+ names.each do |name|
54
+ v.file_exists?(name)
55
+ end
56
+ end
57
+ end
data/test/test_samples.rb CHANGED
@@ -39,3 +39,247 @@ class TestSamples < Test::Unit::TestCase
39
39
  end
40
40
  end
41
41
 
42
+
43
+ class TestUndefined < Test::Unit::TestCase
44
+ include FlexMock::TestCase
45
+
46
+ def test_undefined_values
47
+ m = flexmock("mock")
48
+ m.should_receive(:divide_by).with(0).
49
+ and_return_undefined
50
+ assert_equal FlexMock.undefined, m.divide_by(0)
51
+ end
52
+ end
53
+
54
+
55
+ class TestSimple < Test::Unit::TestCase
56
+ include FlexMock::TestCase
57
+
58
+ def test_simple_mock
59
+ m = flexmock(:pi => 3.1416, :e => 2.71)
60
+ assert_equal 3.1416, m.pi
61
+ assert_equal 2.71, m.e
62
+ end
63
+ end
64
+
65
+ class TestDog < Test::Unit::TestCase
66
+ include FlexMock::TestCase
67
+
68
+ def test_dog_wags
69
+ tail_mock = flexmock(:wag => :happy)
70
+ assert_equal :happy, tail_mock.wag
71
+ end
72
+ end
73
+
74
+ class Woofer
75
+ end
76
+
77
+ class Dog
78
+ def initialize
79
+ @woofer = Woofer.new
80
+ end
81
+ def bark
82
+ @woofer.woof
83
+ end
84
+ def wag
85
+ :happy
86
+ end
87
+ end
88
+
89
+ class TestDogBarking < Test::Unit::TestCase
90
+ include FlexMock::TestCase
91
+
92
+ # Setup the tests by mocking the +new+ method of
93
+ # Woofer and return a mock woofer.
94
+ def setup
95
+ @dog = Dog.new
96
+ flexmock(@dog, :bark => :grrr)
97
+ end
98
+
99
+ def test_dog
100
+ assert_equal :grrr, @dog.bark # Mocked Method
101
+ assert_equal :happy, @dog.wag # Normal Method
102
+ end
103
+ end
104
+
105
+ class TestDogBarkingWithNewInstances < Test::Unit::TestCase
106
+ include FlexMock::TestCase
107
+
108
+ # Setup the tests by mocking Woofer to always
109
+ # return partial mocks.
110
+ def setup
111
+ flexmock(Woofer).new_instances.should_receive(:woof => :grrr)
112
+ end
113
+
114
+ def test_dog
115
+ assert_equal :grrr, Dog.new.bark # All dog objects
116
+ assert_equal :grrr, Dog.new.bark # are mocked.
117
+ end
118
+ end
119
+
120
+ class TestDefaults < Test::Unit::TestCase
121
+ include FlexMock::TestCase
122
+
123
+ def setup
124
+ @mock_dog = flexmock("Fido")
125
+ @mock_dog.should_receive(:tail => :a_tail, :bark => "woof").by_default
126
+ end
127
+
128
+ def test_something_where_bark_must_be_called_once
129
+ @mock_dog.should_receive(:bark => "bow wow").once
130
+
131
+ assert_equal "bow wow", @mock_dog.bark
132
+ assert_equal :a_tail, @mock_dog.tail
133
+ end
134
+ end
135
+
136
+ class TestDemeter < Test::Unit::TestCase
137
+ include FlexMock::TestCase
138
+ def test_manual_mocking
139
+ # Manually mocking a Law of Demeter violation
140
+ cog = flexmock("cog")
141
+ cog.should_receive(:turn).once.and_return(:ok)
142
+ joint = flexmock("gear", :cog => cog)
143
+ axle = flexmock("axle", :universal_joint => joint)
144
+ chassis = flexmock("chassis", :axle => axle)
145
+ car = flexmock("car", :chassis => chassis)
146
+
147
+ # test code
148
+ assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
149
+ end
150
+
151
+ def test_demeter
152
+ car = flexmock("car")
153
+ car.should_receive( "chassis.axle.universal_joint.cog.turn" => :ok).once
154
+
155
+ # Test code
156
+ assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
157
+ end
158
+
159
+ end
160
+
161
+ class TestDb < Test::Unit::TestCase
162
+ include FlexMock::TestCase
163
+
164
+ def test_db
165
+ db = flexmock('db')
166
+ db.should_receive(:query).and_return([1,2,3])
167
+ db.should_receive(:update).with(5).and_return(nil).once
168
+
169
+ # test code
170
+ assert_nil db.update(5)
171
+ end
172
+ end
173
+
174
+
175
+ class TestDb < Test::Unit::TestCase
176
+ include FlexMock::TestCase
177
+
178
+ def test_query_and_update
179
+ db = flexmock('db')
180
+ db.should_receive(:query).and_return([1,2,3]).ordered
181
+ db.should_receive(:update).and_return(nil).ordered
182
+ # test code here
183
+ assert_raises(Test::Unit::AssertionFailedError) do
184
+ db.update
185
+ db.query
186
+ end
187
+ end
188
+
189
+ def test_ordered_queries
190
+ db = flexmock('db')
191
+ db.should_receive(:startup).once.ordered
192
+ db.should_receive(:query).with("CPWR").and_return(12.3).
193
+ once.ordered(:queries)
194
+ db.should_receive(:query).with("MSFT").and_return(10.0).
195
+ once.ordered(:queries)
196
+ db.should_receive(:query).with(/^....$/).and_return(3.3).
197
+ at_least.once.ordered(:queries)
198
+ db.should_receive(:finish).once.ordered
199
+ # test code here
200
+ db.startup
201
+ db.query("CPWR")
202
+ db.query("MSFT")
203
+ db.query("asdf")
204
+ db.finish
205
+ end
206
+
207
+ def test_ordered_queries_in_record_mode
208
+ db = flexmock('db')
209
+ db.should_expect do |rec|
210
+ rec.startup.once.ordered
211
+ rec.query("CPWR") { 12.3 }.once.ordered(:queries)
212
+ rec.query("MSFT") { 10.0 }.once.ordered(:queries)
213
+ rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
214
+ rec.finish.once.ordered
215
+ end
216
+ # test code here using +db+.
217
+ db.startup
218
+ db.query("CPWR")
219
+ db.query("MSFT")
220
+ db.query("asdf")
221
+ db.finish
222
+ end
223
+
224
+ def known_good_way_to_build_xml(builder)
225
+ builder.html
226
+ end
227
+
228
+ def new_way_to_build_xml(builder)
229
+ known_good_way_to_build_xml(builder)
230
+ end
231
+
232
+ def test_build_xml
233
+ builder = flexmock('builder')
234
+ builder.should_expect do |rec|
235
+ rec.should_be_strict
236
+ known_good_way_to_build_xml(rec) # record the messages
237
+ end
238
+ new_way_to_build_xml(builder) # compare to new way
239
+ end
240
+
241
+ end
242
+
243
+ class TestMoreSamples < Test::Unit::TestCase
244
+ include FlexMock::TestCase
245
+
246
+ def test_multiple_gets
247
+ file = flexmock('file')
248
+ file.should_receive(:gets).with_no_args.
249
+ and_return("line 1\n", "line 2\n")
250
+ # test code here
251
+ assert_equal "line 1\n", file.gets
252
+ assert_equal "line 2\n", file.gets
253
+ end
254
+
255
+ def test_an_important_message
256
+ m = flexmock('m')
257
+ m.should_receive(:an_important_message).and_return(1).once
258
+ m.should_ignore_missing
259
+ # test code here
260
+ assert_equal 1, m.an_important_message
261
+ assert_equal FlexMock.undefined, m.other
262
+ end
263
+
264
+ class QuoteService
265
+ end
266
+
267
+ class Portfolio
268
+ def initialize
269
+ @quote_service = QuoteService.new
270
+ end
271
+ def value
272
+ @quote_service.quote
273
+ end
274
+ end
275
+
276
+ def test_portfolio_value
277
+ flexmock(QuoteService).new_instances do |m|
278
+ m.should_receive(:quote).and_return(100)
279
+ end
280
+ port = Portfolio.new
281
+ value = port.value # Portfolio calls QuoteService.quote
282
+ assert_equal 100, value
283
+ end
284
+
285
+ end
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #---
4
+ # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #+++
11
+
12
+ require 'test/unit'
13
+ require 'flexmock'
14
+ require 'test/asserts'
15
+
16
+ class TestShouldIgnoreMissing < Test::Unit::TestCase
17
+ include FlexMock::TestCase
18
+
19
+ def setup
20
+ @mock = flexmock("mock")
21
+ end
22
+
23
+ def test_mocks_do_not_respond_to_undefined_methods
24
+ assert !@mock.respond_to?(:unknown_foo)
25
+ end
26
+
27
+ def test_mocks_do_respond_to_defined_methods
28
+ @mock.should_receive(:known_foo => :bar)
29
+ assert @mock.respond_to?(:known_foo)
30
+ end
31
+
32
+ def test_mocks_do_respond_to_any_method_when_ignoring_missing
33
+ @mock.should_ignore_missing
34
+ assert @mock.respond_to?(:unknown_foo)
35
+ end
36
+
37
+ def test_ignored_methods_return_undefined
38
+ @mock.should_ignore_missing
39
+ assert_equal FlexMock.undefined, @mock.unknown_foo
40
+ @mock.unknown_foo.bar.baz.bleep
41
+ end
42
+
43
+ def test_undefined_mocking_with_arguments
44
+ @mock.should_ignore_missing
45
+ assert_equal FlexMock.undefined, @mock.xyzzy(1,:two,"three")
46
+ end
47
+
48
+ def test_method_chains_with_undefined_are_self_preserving
49
+ @mock.should_ignore_missing
50
+ assert_equal FlexMock.undefined, @mock.a.b.c.d.e.f(1).g.h.i.j
51
+ end
52
+
53
+ def test_method_proc_raises_error_on_unknown
54
+ assert_raises(NameError) {
55
+ @mock.method(:unknown_foo)
56
+ }
57
+ end
58
+
59
+ def test_method_returns_callable_proc
60
+ @mock.should_receive(:known_foo).once
61
+ method_proc = @mock.method(:known_foo)
62
+ assert_not_nil method_proc
63
+ method_proc.call
64
+ end
65
+
66
+ def test_not_calling_method_proc_will_fail_count_constraints
67
+ @mock.should_receive(:known_foo).once
68
+ method_proc = @mock.method(:known_foo)
69
+ assert_not_nil method_proc
70
+ assert_raises Test::Unit::AssertionFailedError do
71
+ flexmock_teardown
72
+ end
73
+ end
74
+
75
+ def test_method_returns_do_nothing_proc_for_missing_methods
76
+ @mock.should_ignore_missing
77
+ method_proc = @mock.method(:plugh)
78
+ assert_not_nil method_proc
79
+ assert_equal FlexMock.undefined, method_proc.call
80
+ end
81
+ end
82
+