flexmock 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README +45 -24
- data/Rakefile +8 -1
- data/lib/flexmock.rb +62 -4
- data/test/test_tu_integration.rb +63 -0
- metadata +6 -4
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
FlexMock is a simple mock object for unit testing. The interface is
|
4
4
|
simple, but still provides a good bit of flexibility.
|
5
5
|
|
6
|
-
Version :: 0.
|
6
|
+
Version :: 0.3.0
|
7
7
|
|
8
8
|
= Links
|
9
9
|
|
@@ -37,14 +37,19 @@ Here's the complete example:
|
|
37
37
|
end
|
38
38
|
|
39
39
|
class TestTemperatureSampler < Test::Unit::TestCase
|
40
|
+
include FlexMock::TestCase
|
41
|
+
|
40
42
|
def test_tempurature_sampler
|
41
43
|
readings = [10, 12, 14]
|
42
|
-
|
43
|
-
|
44
|
+
sensor = flexmock("temp")
|
45
|
+
sensor.should_receive(:read_temperature).and_return { readings.shift }
|
46
|
+
|
47
|
+
sampler = TemperatureSampler.new(sensor)
|
48
|
+
assert_equal 12, sampler.average_temp
|
44
49
|
|
45
|
-
|
46
|
-
|
47
|
-
|
50
|
+
# NOTE:
|
51
|
+
# all mocks created by the +flexmock+ method will be
|
52
|
+
# automatically verified during the test teardown.
|
48
53
|
end
|
49
54
|
end
|
50
55
|
|
@@ -180,19 +185,21 @@ The following rules are used for argument matching:
|
|
180
185
|
|
181
186
|
Examples:
|
182
187
|
|
183
|
-
with(FlexMock.eq(Integer)) will match f(Integer)
|
184
|
-
with(FlexMock.eq(Integer)) will NOT match f(3)
|
185
|
-
|
186
|
-
If the user includes the FlexMock::ArgumentTypes module in the test
|
187
|
-
class, the above examples can be shortened to:
|
188
|
-
|
189
188
|
with(eq(Integer)) will match f(Integer)
|
190
189
|
with(eq(Integer)) will NOT match f(3)
|
191
190
|
|
191
|
+
Note: If you do not use the FlexMock::TestCase Test Unit integration
|
192
|
+
module, or the FlexMock::ArgumentTypes module, you will have to
|
193
|
+
fully qualify the +eq+ method:
|
194
|
+
|
195
|
+
with(FlexMock.eq(Integer)) will match f(Integer)
|
196
|
+
with(FlexMock.eq(Integer)) will NOT match f(3)
|
197
|
+
|
192
198
|
* If you wish to match _anything_, then use the <tt>FlexMock.any</tt>
|
193
199
|
method in the with argument list.
|
194
200
|
|
195
|
-
Examples (assumes FlexMock::
|
201
|
+
Examples (assumes either the FlexMock::TestCase or
|
202
|
+
FlexMock::ArgumentTypes mix-ins has been included):
|
196
203
|
|
197
204
|
with(any) will match f(3)
|
198
205
|
with(any) will match f("hello")
|
@@ -215,17 +222,26 @@ The following rules are used for argument matching:
|
|
215
222
|
The queries my have any arguments. The update must have a specific
|
216
223
|
argument of 5.
|
217
224
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
225
|
+
class TestDb
|
226
|
+
include FlexMock::TestCase
|
227
|
+
|
228
|
+
def test_db
|
229
|
+
db = flexmock('db')
|
230
|
+
db.should_receive(:query).and_return([1,2,3])
|
231
|
+
db.should_receive(:update).with(5).and_return(nil).once
|
232
|
+
# test code here
|
233
|
+
end
|
222
234
|
end
|
223
235
|
|
224
236
|
=== Expect all queries before any updates
|
225
237
|
|
238
|
+
(This and following examples assume that the FlexMock::TestCase module
|
239
|
+
is being used.)
|
240
|
+
|
226
241
|
All the query message must occur before any of the update messages.
|
227
242
|
|
228
|
-
|
243
|
+
def test_query_and_update
|
244
|
+
db = flexmock('db')
|
229
245
|
db.should_receive(:query).and_return([1,2,3]).ordered
|
230
246
|
db.should_recieve(:update).and_return(nil).ordered
|
231
247
|
# test code here
|
@@ -243,7 +259,8 @@ Startup and finish must also happen exactly once.
|
|
243
259
|
Also note that we use the +with+ method to match different arguement
|
244
260
|
values to figure out what value to return.
|
245
261
|
|
246
|
-
|
262
|
+
def test_ordered_queries
|
263
|
+
db = flexmock('db')
|
247
264
|
db.should_receive(:startup).once.ordered
|
248
265
|
db.should_receive(:query).with("CPWR").and_return(12.3).
|
249
266
|
once.ordered(:queries)
|
@@ -262,7 +279,8 @@ The record mode interface offers much the same features as the
|
|
262
279
|
messages to be sent directly to a recording object rather than be
|
263
280
|
specified indirectly using a symbol.
|
264
281
|
|
265
|
-
|
282
|
+
def test_ordered_queries_in_record_mode
|
283
|
+
db = flexmock('db')
|
266
284
|
db.should_expect do |rec|
|
267
285
|
rec.startup.once.ordered
|
268
286
|
rec.query("CPWR") { 12.3 }.once.ordered(:queries)
|
@@ -286,7 +304,8 @@ matching on argument lists, and strict ordering of the method calls.
|
|
286
304
|
objects, because the query responses cannot be programmed into the
|
287
305
|
recorder object.
|
288
306
|
|
289
|
-
|
307
|
+
def test_build_xml
|
308
|
+
builder = flexmock('builder')
|
290
309
|
builder.should_expect do |rec|
|
291
310
|
rec.should_be_strict
|
292
311
|
known_good_way_to_build_xml(rec) # record the messages
|
@@ -300,7 +319,8 @@ Sometimes you need to return different values for each call to a
|
|
300
319
|
mocked method. This example shifts values out of a list for this
|
301
320
|
effect.
|
302
321
|
|
303
|
-
|
322
|
+
def test_multiple_gets
|
323
|
+
file = flexmock('file')
|
304
324
|
return_values = ["line 1\n", "line 2\n"]
|
305
325
|
file.should_receive(:gets).with_no_args.and_return { return_values.shift }
|
306
326
|
# test code here
|
@@ -313,7 +333,8 @@ interesting value or wish to assert were sent in a particular manner.
|
|
313
333
|
Use the +should_ignore_missing+ method to turn on missing method
|
314
334
|
ignoring.
|
315
335
|
|
316
|
-
|
336
|
+
def test_an_important_message
|
337
|
+
m = flexmock('m')
|
317
338
|
m.should_recieve(:an_important_message).and_return(1).once
|
318
339
|
m.should_ignore_missing
|
319
340
|
# test code here
|
@@ -339,7 +360,7 @@ test-unit-mock :: http://www.deveiate.org/code/Test-Unit-Mock.shtml
|
|
339
360
|
|
340
361
|
== License
|
341
362
|
|
342
|
-
Copyright 2003, 2004, 2005 by Jim Weirich (jim@weirichhouse.org).
|
363
|
+
Copyright 2003, 2004, 2005, 2006 by Jim Weirich (jim@weirichhouse.org).
|
343
364
|
All rights reserved.
|
344
365
|
|
345
366
|
Permission is granted for use, copying, modification, distribution,
|
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ require 'rake/testtask'
|
|
8
8
|
|
9
9
|
CLOBBER.include("html", 'pkg')
|
10
10
|
|
11
|
-
PKG_VERSION = '0.
|
11
|
+
PKG_VERSION = '0.3.0'
|
12
12
|
|
13
13
|
PKG_FILES = FileList[
|
14
14
|
'[A-Z]*',
|
@@ -137,6 +137,13 @@ end
|
|
137
137
|
|
138
138
|
SVNHOME = 'svn://localhost/software/flexmock'
|
139
139
|
|
140
|
+
task :specs do
|
141
|
+
specs = FileList['test/spec_*.rb']
|
142
|
+
ENV['RUBYLIB'] = "lib:test:#{ENV['RUBYLIB']}"
|
143
|
+
sh %{spec #{specs}}
|
144
|
+
end
|
145
|
+
|
140
146
|
task :tag do
|
141
147
|
sh %{svn copy #{SVNHOME}/trunk #{SVNHOME}/tags/rel-#{PKG_VERSION} -m 'Release #{PKG_VERSION}'}
|
142
148
|
end
|
149
|
+
|
data/lib/flexmock.rb
CHANGED
@@ -30,9 +30,22 @@ require 'test/unit'
|
|
30
30
|
# m.should_receive(:downcase).with(String).
|
31
31
|
# returns { |s| s.downcase }.once
|
32
32
|
#
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
# With Test::Unit Integration:
|
34
|
+
#
|
35
|
+
# class TestSomething < Test::Unit::TestCase
|
36
|
+
# include FlexMock::TestCase
|
37
|
+
#
|
38
|
+
# def test_something
|
39
|
+
# m = flexmock("name")
|
40
|
+
# m.should_receive(:hi).and_return("Hello")
|
41
|
+
# m.hi
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# Note: When using Test::Unit integeration, don't forget to include
|
46
|
+
# FlexMock::TestCase. Also, if you override +teardown+, make sure you
|
47
|
+
# call +super+.
|
48
|
+
#
|
36
49
|
class FlexMock
|
37
50
|
include Test::Unit::Assertions
|
38
51
|
|
@@ -156,6 +169,10 @@ class FlexMock
|
|
156
169
|
# returns(0).once
|
157
170
|
# end # mock is verified here
|
158
171
|
#
|
172
|
+
# NOTE: If you include FlexMock::TestCase into your test case
|
173
|
+
# file, you can create moks that will be automatically verified in
|
174
|
+
# the test teardown by using the +flexmock+ method.
|
175
|
+
#
|
159
176
|
def use(*names)
|
160
177
|
names = ["unknown"] if names.empty?
|
161
178
|
got_excecption = false
|
@@ -313,6 +330,7 @@ class FlexMock
|
|
313
330
|
ProcMatcher.new(&block)
|
314
331
|
end
|
315
332
|
end
|
333
|
+
extend ArgumentTypes
|
316
334
|
|
317
335
|
####################################################################
|
318
336
|
# Base class for all the count validators.
|
@@ -648,5 +666,45 @@ class FlexMock
|
|
648
666
|
end
|
649
667
|
end
|
650
668
|
|
651
|
-
|
669
|
+
####################################################################
|
670
|
+
# Test::Unit::TestCase Integration.
|
671
|
+
#
|
672
|
+
# Include this module in any TestCase class in a Test::Unit test
|
673
|
+
# suite to get integration with FlexMock. When this module is
|
674
|
+
# included, mocks may be created with a simple call to the
|
675
|
+
# +flexmock+ method. Mocks created with via the method call will
|
676
|
+
# automatically be verified in the teardown of the test case.
|
677
|
+
#
|
678
|
+
# <b>Note:</b> If you define a +teardown+ method in the test case,
|
679
|
+
# <em>dont' forget to invoke the +super+ method!</em> Failure to
|
680
|
+
# invoke super will cause all mocks to not be verified.
|
681
|
+
#
|
682
|
+
module TestCase
|
683
|
+
include ArgumentTypes
|
684
|
+
|
685
|
+
# Teardown the test case, verifying any mocks that might have been
|
686
|
+
# defined in this test case.
|
687
|
+
def teardown
|
688
|
+
super
|
689
|
+
flexmock_teardown
|
690
|
+
end
|
691
|
+
|
692
|
+
# Do the flexmock specific teardown stuff.
|
693
|
+
def flexmock_teardown
|
694
|
+
return if @flexmock_created_mocks.nil?
|
695
|
+
@flexmock_created_mocks.each do |m|
|
696
|
+
m.mock_verify
|
697
|
+
end
|
698
|
+
end
|
699
|
+
|
700
|
+
# Create a FlexMock object with the given name. Mocks created
|
701
|
+
# with this method will be automatically verify during teardown
|
702
|
+
# (assuming the the flexmock teardown isn't overridden).
|
703
|
+
def flexmock(name="unknown")
|
704
|
+
result = FlexMock.new(name)
|
705
|
+
@flexmock_created_mocks ||= []
|
706
|
+
@flexmock_created_mocks << result
|
707
|
+
result
|
708
|
+
end
|
709
|
+
end
|
652
710
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'flexmock'
|
5
|
+
|
6
|
+
class TestTuIntegrationMockVerificationInTeardown < Test::Unit::TestCase
|
7
|
+
include FlexMock::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_mock_verification_occurs_during_teardown
|
20
|
+
flexmock("xyz").should_receive(:hi).with(any).once
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class TestTuIntegrationMockVerificationWithoutSetup < Test::Unit::TestCase
|
25
|
+
include FlexMock::TestCase
|
26
|
+
|
27
|
+
def teardown
|
28
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_mock_verification_occurs_during_teardown
|
34
|
+
flexmock("xyz").should_receive(:hi).with(any).once
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class TestTuIntegrationMockVerificationForgetfulSetup < Test::Unit::TestCase
|
39
|
+
include FlexMock::TestCase
|
40
|
+
|
41
|
+
def setup
|
42
|
+
end
|
43
|
+
|
44
|
+
def teardown
|
45
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_mock_verification_occurs_during_teardown
|
51
|
+
flexmock("xyz").should_receive(:hi).with(any).once
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class TestTuIntegrationSetupOverridenAndNoMocksOk < Test::Unit::TestCase
|
56
|
+
include FlexMock::TestCase
|
57
|
+
|
58
|
+
def setup
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_mock_verification_occurs_during_teardown
|
62
|
+
end
|
63
|
+
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.11.
|
2
|
+
rubygems_version: 0.8.11.15
|
3
3
|
specification_version: 1
|
4
4
|
name: flexmock
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2006-
|
6
|
+
version: 0.3.0
|
7
|
+
date: 2006-05-14 00:00:00 -04:00
|
8
8
|
summary: Simple and Flexible Mock Objects for Testing
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
25
25
|
platform: ruby
|
26
26
|
signing_key:
|
27
27
|
cert_chain:
|
28
|
+
post_install_message:
|
28
29
|
authors:
|
29
30
|
- Jim Weirich
|
30
31
|
files:
|
@@ -32,12 +33,13 @@ files:
|
|
32
33
|
- Rakefile
|
33
34
|
- README
|
34
35
|
- lib/flexmock.rb
|
35
|
-
- test/test_should_receive.rb
|
36
36
|
- test/test_record_mode.rb
|
37
|
+
- test/test_should_receive.rb
|
37
38
|
- test/test_samples.rb
|
38
39
|
- test/test_mock.rb
|
39
40
|
- test/test_naming.rb
|
40
41
|
- test/test_example.rb
|
42
|
+
- test/test_tu_integration.rb
|
41
43
|
- flexmock.blurb
|
42
44
|
- install.rb
|
43
45
|
test_files: []
|