flexmock 0.6.0 → 0.6.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.
- data/CHANGELOG +5 -0
- data/README +4 -4
- data/Rakefile +1 -1
- data/doc/releases/flexmock-0.6.0.rdoc +1 -1
- data/doc/releases/flexmock-0.6.1.rdoc +91 -0
- data/lib/flexmock/core.rb +0 -1
- data/lib/flexmock/partial_mock.rb +29 -24
- data/test/test_new_instances.rb +24 -0
- metadata +4 -2
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
FlexMock is a simple, but flexible, mock object library for Ruby unit
|
4
4
|
testing.
|
5
5
|
|
6
|
-
Version :: 0.6.
|
6
|
+
Version :: 0.6.1
|
7
7
|
|
8
8
|
= Links
|
9
9
|
|
@@ -155,7 +155,7 @@ expectation and adds constraints to that.
|
|
155
155
|
|
156
156
|
For example, the following code:
|
157
157
|
|
158
|
-
mock.
|
158
|
+
mock.should_receive(:average).and_return(12)
|
159
159
|
|
160
160
|
Means that the mock will now accept method calls to an +average+ method. The
|
161
161
|
expectation will accept any arguments and may be called any number of times
|
@@ -545,7 +545,7 @@ All the query message must occur before any of the update messages.
|
|
545
545
|
def test_query_and_update
|
546
546
|
db = flexmock('db')
|
547
547
|
db.should_receive(:query).and_return([1,2,3]).ordered
|
548
|
-
db.
|
548
|
+
db.should_receive(:update).and_return(nil).ordered
|
549
549
|
# test code here
|
550
550
|
end
|
551
551
|
|
@@ -637,7 +637,7 @@ ignoring.
|
|
637
637
|
|
638
638
|
def test_an_important_message
|
639
639
|
m = flexmock('m')
|
640
|
-
m.
|
640
|
+
m.should_receive(:an_important_message).and_return(1).once
|
641
641
|
m.should_ignore_missing
|
642
642
|
# test code here
|
643
643
|
end
|
data/Rakefile
CHANGED
@@ -32,7 +32,7 @@ FlexMock is a flexible mocking library for use in unit testing and behavior spec
|
|
32
32
|
flexmock.should_receive(:read, :write).at_least.once
|
33
33
|
|
34
34
|
|
35
|
-
* +
|
35
|
+
* +should_receive+ now will allow expectation hashes as arguments. This is
|
36
36
|
similar to the list of methods, but allows each defined method to have its
|
37
37
|
own return value.
|
38
38
|
|
@@ -0,0 +1,91 @@
|
|
1
|
+
= FlexMock 0.6.1 Released
|
2
|
+
|
3
|
+
FlexMock is a flexible mocking library for use in unit testing and behavior
|
4
|
+
specification in Ruby. Version 0.6.1 introduces a number minor bug fixes.
|
5
|
+
|
6
|
+
== New in 0.6.1
|
7
|
+
|
8
|
+
* Fixed several bugs related to partial mocks and ordering and mocking field
|
9
|
+
assignments.
|
10
|
+
|
11
|
+
== What is FlexMock?
|
12
|
+
|
13
|
+
FlexMock is a flexible framework for creating mock object for testing. When
|
14
|
+
running unit tests, it is often desirable to use isolate the objects being
|
15
|
+
tested from the "real world" by having them interact with simplified test
|
16
|
+
objects. Sometimes these test objects simply return values when called, other
|
17
|
+
times they verify that certain methods were called with particular arguments
|
18
|
+
in a particular order.
|
19
|
+
|
20
|
+
FlexMock makes creating these test objects easy.
|
21
|
+
|
22
|
+
=== Features
|
23
|
+
|
24
|
+
* Easy integration with both Test::Unit and RSpec. Mocks created with the
|
25
|
+
flexmock method are automatically verified at the end of the test or
|
26
|
+
example.
|
27
|
+
|
28
|
+
* A fluent interface that allows mock behavior to be specified very
|
29
|
+
easily.
|
30
|
+
|
31
|
+
* A "record mode" where an existing implementation can record its
|
32
|
+
interaction with a mock for later validation against a new
|
33
|
+
implementation.
|
34
|
+
|
35
|
+
* Easy mocking of individual methods in existing, non-mock objects.
|
36
|
+
|
37
|
+
* The ability to cause classes to instantiate test instances (instead of real
|
38
|
+
instances) for the duration of a test.
|
39
|
+
|
40
|
+
=== Example
|
41
|
+
|
42
|
+
Suppose you had a Dog object that wagged a tail when it was happy.
|
43
|
+
Something like this:
|
44
|
+
|
45
|
+
class Dog
|
46
|
+
def initialize(a_tail)
|
47
|
+
@tail = a_tail
|
48
|
+
end
|
49
|
+
def happy
|
50
|
+
@tail.wag
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
To test the +Dog+ class without a real +Tail+ object (perhaps because
|
55
|
+
real +Tail+ objects activate servos in some robotic equipment), you
|
56
|
+
can do something like this:
|
57
|
+
|
58
|
+
require 'test/unit'
|
59
|
+
require 'flexmock/test_unit'
|
60
|
+
|
61
|
+
class TestDog < Test::Unit::TestCase
|
62
|
+
def test_dog_wags_tail_when_happy
|
63
|
+
tail = flexmock("tail")
|
64
|
+
tail.should_receive(:wag).once
|
65
|
+
dog = Dog.new(tail)
|
66
|
+
dog.happy
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
FlexMock will automatically verify that the mocked tail object received the
|
71
|
+
message +wag+ exactly one time. If it doesn't, the test will not pass.
|
72
|
+
|
73
|
+
See the FlexMock documentation at http://flexmock.rubyforge.org for details on
|
74
|
+
specifying arguments and return values on mocked methods, as well as a simple
|
75
|
+
technique for mocking tail objects when the Dog class creates the tail objects
|
76
|
+
directly.
|
77
|
+
|
78
|
+
== Availability
|
79
|
+
|
80
|
+
You can make sure you have the latest version with a quick RubyGems command:
|
81
|
+
|
82
|
+
gem install flexmock (you may need root/admin privileges)
|
83
|
+
|
84
|
+
Otherwise, you can get it from the more traditional places:
|
85
|
+
|
86
|
+
Download:: http://rubyforge.org/project/showfiles.php?group_id=170
|
87
|
+
|
88
|
+
You will find documentation at: http://flexmock.rubyforge.org.
|
89
|
+
|
90
|
+
-- Jim Weirich
|
91
|
+
|
data/lib/flexmock/core.rb
CHANGED
@@ -13,19 +13,18 @@ require 'flexmock/noop'
|
|
13
13
|
|
14
14
|
class FlexMock
|
15
15
|
|
16
|
-
|
17
|
-
# PartialMock is used to mate the mock framework to an existing
|
18
|
-
# object
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# mocked (e.g. remove instance variables and mock singleton
|
25
|
-
# methods).
|
16
|
+
# #########################################################################
|
17
|
+
# PartialMock is used to mate the mock framework to an existing object. The
|
18
|
+
# object is "enhanced" with a reference to a mock object (stored in
|
19
|
+
# <tt>@flexmock_mock</tt>). When the +should_receive+ method is sent to the
|
20
|
+
# proxy, it overrides the existing object's method by creating singleton
|
21
|
+
# method that forwards to the mock. When testing is complete, PartialMock
|
22
|
+
# will erase the mocking infrastructure from the object being mocked (e.g.
|
23
|
+
# remove instance variables and mock singleton methods).
|
26
24
|
#
|
27
25
|
class PartialMock
|
28
|
-
attr_reader :mock
|
26
|
+
attr_reader :mock, :mock_groups
|
27
|
+
attr_accessor :mock_current_order, :mock_container
|
29
28
|
|
30
29
|
# Initialize a PartialMock object.
|
31
30
|
def initialize(obj, mock)
|
@@ -33,6 +32,9 @@ class FlexMock
|
|
33
32
|
@mock = mock
|
34
33
|
@method_definitions = {}
|
35
34
|
@methods_proxied = []
|
35
|
+
@allocated_order = 0
|
36
|
+
@mock_current_order = 0
|
37
|
+
@mock_groups = {}
|
36
38
|
end
|
37
39
|
|
38
40
|
# :call-seq:
|
@@ -132,16 +134,9 @@ class FlexMock
|
|
132
134
|
end
|
133
135
|
end
|
134
136
|
|
135
|
-
#
|
136
|
-
|
137
|
-
|
138
|
-
def mock_container
|
139
|
-
@mock.mock_container
|
140
|
-
end
|
141
|
-
|
142
|
-
# Set the container for this mock object.
|
143
|
-
def mock_container=(container)
|
144
|
-
@mock.mock_container = container
|
137
|
+
# Allocation a new order number from the mock.
|
138
|
+
def mock_allocate_order
|
139
|
+
@allocated_order += 1
|
145
140
|
end
|
146
141
|
|
147
142
|
private
|
@@ -189,9 +184,19 @@ class FlexMock
|
|
189
184
|
# proxy method is defined as a singleton method on the object
|
190
185
|
# being mocked.
|
191
186
|
def define_proxy_method(method_name)
|
192
|
-
|
193
|
-
|
194
|
-
|
187
|
+
if method_name.to_s =~ /=$/
|
188
|
+
sclass.class_eval %{
|
189
|
+
def #{method_name}(*args, &block)
|
190
|
+
@flexmock_proxy.mock.__send__(:#{method_name}, *args, &block)
|
191
|
+
end
|
192
|
+
}
|
193
|
+
else
|
194
|
+
sclass.class_eval %{
|
195
|
+
def #{method_name}(*args, &block)
|
196
|
+
@flexmock_proxy.mock.#{method_name}(*args, &block)
|
197
|
+
end
|
198
|
+
}
|
199
|
+
end
|
195
200
|
end
|
196
201
|
|
197
202
|
# Restore the original singleton defintion for method_name that
|
data/test/test_new_instances.rb
CHANGED
@@ -191,6 +191,30 @@ class TestNewInstances < Test::Unit::TestCase
|
|
191
191
|
assert_equal :grrr, Dog.new.woof
|
192
192
|
end
|
193
193
|
|
194
|
+
def test_writable_accessors
|
195
|
+
flexmock(Dog).new_instances.should_receive(:name=).with("fido")
|
196
|
+
dog = Dog.new
|
197
|
+
dog.name = 'fido'
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_ordering_can_be_specified
|
201
|
+
dog = Dog.new
|
202
|
+
flexmock(dog).should_receive(:bark).once.ordered
|
203
|
+
flexmock(dog).should_receive(:bite).once.ordered
|
204
|
+
dog.bark
|
205
|
+
dog.bite
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_ordering_can_be_specified_in_groups
|
209
|
+
dog = Dog.new
|
210
|
+
flexmock(dog).should_receive(:wag).once.ordered(:safe)
|
211
|
+
flexmock(dog).should_receive(:bark).once.ordered(:danger)
|
212
|
+
flexmock(dog).should_receive(:bite).once.ordered(:danger)
|
213
|
+
dog.wag
|
214
|
+
dog.bite
|
215
|
+
dog.bark
|
216
|
+
end
|
217
|
+
|
194
218
|
def redirect_error
|
195
219
|
require 'stringio'
|
196
220
|
old_err = $stderr
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: flexmock
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.6.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.6.1
|
7
|
+
date: 2007-05-08 00:00:00 -04:00
|
8
8
|
summary: Simple and Flexible Mock Objects for Testing
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- doc/releases/flexmock-0.5.0.rdoc
|
75
75
|
- doc/releases/flexmock-0.5.1.rdoc
|
76
76
|
- doc/releases/flexmock-0.6.0.rdoc
|
77
|
+
- doc/releases/flexmock-0.6.1.rdoc
|
77
78
|
test_files: []
|
78
79
|
|
79
80
|
rdoc_options:
|
@@ -93,6 +94,7 @@ extra_rdoc_files:
|
|
93
94
|
- doc/releases/flexmock-0.5.0.rdoc
|
94
95
|
- doc/releases/flexmock-0.5.1.rdoc
|
95
96
|
- doc/releases/flexmock-0.6.0.rdoc
|
97
|
+
- doc/releases/flexmock-0.6.1.rdoc
|
96
98
|
executables: []
|
97
99
|
|
98
100
|
extensions: []
|