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.
- checksums.yaml +4 -4
- data/.autotest +3 -0
- data/.gitignore +14 -0
- data/.togglerc +7 -0
- data/.travis.yml +5 -0
- data/.yardopts +2 -0
- data/CHANGES +11 -0
- data/Gemfile +1 -4
- data/README.md +39 -11
- data/Rakefile +6 -217
- data/doc/examples/rspec_examples_spec.rb +244 -0
- data/doc/examples/test_unit_examples_test.rb +240 -0
- data/doc/jamis.rb +591 -0
- data/flexmock.gemspec +33 -0
- data/lib/flexmock.rb +0 -1
- data/lib/flexmock/composite_expectation.rb +1 -1
- data/lib/flexmock/core.rb +3 -7
- data/lib/flexmock/core_class_methods.rb +5 -1
- data/lib/flexmock/default_framework_adapter.rb +2 -2
- data/lib/flexmock/expectation.rb +29 -3
- data/lib/flexmock/expectation_director.rb +1 -1
- data/lib/flexmock/minitest.rb +13 -0
- data/lib/flexmock/minitest_extensions.rb +26 -0
- data/lib/flexmock/minitest_integration.rb +111 -0
- data/lib/flexmock/mock_container.rb +1 -2
- data/lib/flexmock/partial_mock.rb +61 -104
- data/lib/flexmock/recorder.rb +1 -2
- data/lib/flexmock/rspec.rb +6 -3
- data/lib/flexmock/test_unit_integration.rb +14 -0
- data/lib/flexmock/validators.rb +5 -4
- data/lib/flexmock/version.rb +1 -9
- data/rakelib/metrics.rake +40 -0
- data/rakelib/preview.rake +4 -0
- data/rakelib/tags.rake +18 -0
- data/todo.txt +20 -0
- metadata +61 -86
- data/Gemfile.lock +0 -20
- data/doc/examples/rspec_examples_spec.rdoc +0 -245
- data/doc/examples/test_unit_examples_test.rdoc +0 -241
- data/test/aliasing_test.rb +0 -66
- data/test/assert_spy_called_test.rb +0 -119
- data/test/base_class_test.rb +0 -71
- data/test/based_partials_test.rb +0 -51
- data/test/container_methods_test.rb +0 -118
- data/test/default_framework_adapter_test.rb +0 -38
- data/test/demeter_mocking_test.rb +0 -191
- data/test/deprecated_methods_test.rb +0 -225
- data/test/examples_from_readme_test.rb +0 -157
- data/test/expectation_description_test.rb +0 -80
- data/test/extended_should_receive_test.rb +0 -69
- data/test/flexmodel_test.rb +0 -54
- data/test/mock_builder_test.rb +0 -68
- data/test/naming_test.rb +0 -84
- data/test/new_instances_test.rb +0 -215
- data/test/object_extensions_test.rb +0 -25
- data/test/partial_mock_test.rb +0 -458
- data/test/record_mode_test.rb +0 -158
- data/test/redirect_error.rb +0 -16
- data/test/rspec_integration/integration_spec.rb +0 -56
- data/test/rspec_integration/spy_example_spec.rb +0 -207
- data/test/samples_test.rb +0 -283
- data/test/should_ignore_missing_test.rb +0 -84
- data/test/should_receive_test.rb +0 -1155
- data/test/spys_test.rb +0 -215
- data/test/symbol_extensions_test.rb +0 -8
- data/test/test_class_extensions.rb +0 -34
- data/test/test_setup.rb +0 -92
- data/test/test_unit_integration/auto_test_unit_test.rb +0 -42
- data/test/test_unit_integration/minitest_teardown_test.rb +0 -14
- data/test/tu_integration_test.rb +0 -99
- data/test/undefined_test.rb +0 -87
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ade2170748d406102f5fb790cf747729faef19b
|
4
|
+
data.tar.gz: af92a2e818928d228f710c3e05b9d76267603c76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efd2242d6a87e8c35a7656f1612e00b8d653875ba10a9d47c4353ff8157b05ace064fa98ec78cafb35ec6d1f30ccf28167a192d06a6fd75556de6bdcce3429ab
|
7
|
+
data.tar.gz: 7fdb2eafeca01e7f5a3e88234d75f7274846ce2b4d1d79c063990713cb4bff1c24b825ba408324377bf0de558541a25e2ebcc8616d0ea39f0b5e6d7f555c3f5e
|
data/.autotest
ADDED
data/.gitignore
ADDED
data/.togglerc
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CHANGES
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
= Changes for FlexMock
|
2
2
|
|
3
|
+
== Version 2.0.0
|
4
|
+
|
5
|
+
* bump to 2.0 to mark the change of maintainership. I hope I won't disappoint.
|
6
|
+
* require 'flexmock' no longer pulls test_unit_integration. Require the latter
|
7
|
+
explicitely
|
8
|
+
* dropped support for pre-2.0 Rubies
|
9
|
+
* added explicit minitest support. This makes the minitest integration much
|
10
|
+
nicer to work with (in particular, the flexmock_teardown is now executed "just
|
11
|
+
at the right place"), and fixes issues with minitest 5
|
12
|
+
* partial mocks now supports prepended modules
|
13
|
+
|
3
14
|
== Version 1.0.0
|
4
15
|
|
5
16
|
* Added spy support.
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
# Flex Mock -- Making Mocking Easy
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/doudou/flexmock.svg?branch=master)](https://travis-ci.org/doudou/flexmock)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/flexmock.svg)](http://badge.fury.io/rb/flexmock)
|
5
|
+
|
3
6
|
FlexMock is a simple, but flexible, mock object library for Ruby unit
|
4
7
|
testing.
|
5
8
|
|
6
|
-
Version ::
|
9
|
+
Version :: 2.0
|
7
10
|
|
8
11
|
# Links
|
9
12
|
|
10
|
-
* **Documents** -- http://
|
13
|
+
* **Documents** -- http://rubydoc.info/gems/flemxock
|
11
14
|
* **RubyGems** -- Install with: `gem install flexmock`
|
12
|
-
* **Source** -- https://github.com/
|
13
|
-
* **Bug Reports / Issue Tracking** -- https://github.com/
|
14
|
-
* **Continuous Integration** -- http://travis-ci.org/#!/
|
15
|
+
* **Source** -- https://github.com/doudou/flexmock
|
16
|
+
* **Bug Reports / Issue Tracking** -- https://github.com/doudou/flexmock/issues
|
17
|
+
* **Continuous Integration** -- http://travis-ci.org/#!/doudou/flexmock
|
15
18
|
|
16
19
|
## Installation
|
17
20
|
|
@@ -64,6 +67,33 @@ You can find an extended example of FlexMock in
|
|
64
67
|
[Google Example](http://flexmock.rubyforge.org/files/doc/GoogleExample_rdoc.html
|
65
68
|
"Example").
|
66
69
|
|
70
|
+
## Minitest Integration
|
71
|
+
|
72
|
+
FlexMock integrates nicely with Minitest. Just require the
|
73
|
+
'flexmock/minitest' file at the top of your test file. The
|
74
|
+
`flexmock` method will be available for mock creation, and
|
75
|
+
any created mocks will be automatically validated and closed at the
|
76
|
+
end of the individual test. It works with both tests unit-style
|
77
|
+
(subclasses of Minitest::Test) and spec-style.
|
78
|
+
|
79
|
+
Your test case will look something like this:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
require 'flexmock/minitest'
|
83
|
+
|
84
|
+
class TestDog < Minitest::Test
|
85
|
+
def test_dog_wags
|
86
|
+
tail_mock = flexmock(:wag => :happy)
|
87
|
+
assert_equal :happy, tail_mock.wag
|
88
|
+
end
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
**NOTE:** If you don't want to automatically extend every Minitest::Test
|
93
|
+
with the flexmock methods and overhead, then require the 'flexmock'
|
94
|
+
file and explicitly include the FlexMock::Minitest module in each test
|
95
|
+
case class where you wish to use mock objects.
|
96
|
+
|
67
97
|
## Test::Unit Integration
|
68
98
|
|
69
99
|
FlexMock integrates nicely with Test::Unit. Just require the
|
@@ -1168,16 +1198,14 @@ Refer to the following documents for examples of using FlexMock:
|
|
1168
1198
|
## License
|
1169
1199
|
|
1170
1200
|
Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
|
1171
|
-
|
1201
|
+
Copyright 2014- by Sylvain Joyeux (sylvain.joyeux@m4x.org)
|
1172
1202
|
|
1173
|
-
|
1174
|
-
and distribution of modified versions of this work as long as the
|
1175
|
-
above copyright notice is included.
|
1203
|
+
Licensed under [the MIT license](http://opensource.org/licenses/MIT)
|
1176
1204
|
|
1177
1205
|
# Other stuff
|
1178
1206
|
|
1179
|
-
* **Author** -- Jim Weirich <jim.weirich@gmail.com>
|
1180
|
-
* **Requires** -- Ruby
|
1207
|
+
* **Author** -- Jim Weirich <jim.weirich@gmail.com> and Sylvain Joyeux <sylvain.joyeux@m4x.org>
|
1208
|
+
* **Requires** -- Ruby 2.0 or later
|
1181
1209
|
|
1182
1210
|
## See Also
|
1183
1211
|
|
data/Rakefile
CHANGED
@@ -1,221 +1,10 @@
|
|
1
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
task :noop
|
12
|
-
require 'rubygems'
|
13
|
-
require 'rake/clean'
|
14
|
-
require 'rake/testtask'
|
15
|
-
require 'rake/contrib/rubyforgepublisher'
|
16
|
-
|
17
|
-
require 'rubygems/package_task'
|
18
|
-
|
19
|
-
CLEAN.include('*.tmp')
|
20
|
-
CLOBBER.include("html", 'pkg')
|
21
|
-
|
22
|
-
load './lib/flexmock/version.rb'
|
23
|
-
|
24
|
-
PKG_VERSION = FlexMock::VERSION
|
25
|
-
|
26
|
-
EXAMPLE_RB = FileList['doc/examples/*.rb']
|
27
|
-
EXAMPLE_DOC = EXAMPLE_RB.ext('rdoc')
|
28
|
-
|
29
|
-
CLOBBER.include(EXAMPLE_DOC)
|
30
|
-
CLEAN.include('pkg/flexmock-*').exclude("pkg/*.gem")
|
31
|
-
|
32
|
-
PKG_FILES = FileList[
|
33
|
-
'[A-Z]*',
|
34
|
-
'lib/**/*.rb',
|
35
|
-
'test/**/*.rb',
|
36
|
-
'*.blurb',
|
37
|
-
'install.rb'
|
38
|
-
]
|
39
|
-
|
40
|
-
RDOC_FILES = FileList[
|
41
|
-
'doc/index.rdoc',
|
42
|
-
'CHANGES',
|
43
|
-
'lib/**/*.rb',
|
44
|
-
'doc/**/*.rdoc',
|
45
|
-
] + EXAMPLE_DOC
|
46
|
-
|
47
|
-
task :default => [:test_all, :rspec, :testunit]
|
48
|
-
task :test_all => [:test]
|
49
|
-
task :test_units => [:test]
|
50
|
-
task :ta => [:test_all]
|
51
|
-
|
52
|
-
# Test Targets -------------------------------------------------------
|
53
|
-
|
54
|
-
Rake::TestTask.new do |t|
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
55
7
|
t.test_files = FileList['test/*_test.rb']
|
56
|
-
t.libs << "."
|
57
|
-
t.verbose = false
|
58
|
-
t.warning = true
|
59
8
|
end
|
60
9
|
|
61
|
-
|
62
|
-
def self.minitest?
|
63
|
-
require 'minitest/autorun'
|
64
|
-
return true
|
65
|
-
rescue Exception
|
66
|
-
return false
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
task :testunit do
|
71
|
-
files = FileList['test/test_unit_integration/*_test.rb']
|
72
|
-
if ! Configuration.minitest?
|
73
|
-
files = files.reject { |fn| fn =~ /minitest/ }
|
74
|
-
end
|
75
|
-
files.each do |file|
|
76
|
-
sh "ruby -Ilib:. #{file}"
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
task :rspec do
|
81
|
-
sh "rspec test/rspec_integration"
|
82
|
-
end
|
83
|
-
|
84
|
-
# RCov Target --------------------------------------------------------
|
85
|
-
|
86
|
-
begin
|
87
|
-
require 'rcov/rcovtask'
|
88
|
-
|
89
|
-
Rcov::RcovTask.new do |t|
|
90
|
-
t.libs << "test"
|
91
|
-
t.rcov_opts = ['-xRakefile', '-xrakefile', '-xpublish.rf', '-x/Lib*', '--text-report', '--sort', 'coverage']
|
92
|
-
t.test_files = FileList['test/test*.rb']
|
93
|
-
t.verbose = true
|
94
|
-
end
|
95
|
-
rescue LoadError => ex
|
96
|
-
end
|
97
|
-
|
98
|
-
# RDoc Target --------------------------------------------------------
|
99
|
-
|
100
|
-
task :rdoc => ["html/index.html", :fixcss]
|
101
|
-
|
102
|
-
file "html/index.html" => ["Rakefile"] + RDOC_FILES do
|
103
|
-
Bundler.with_clean_env do
|
104
|
-
sh "rdoc -o html --title FlexMock --line-numbers -m doc/index.rdoc #{RDOC_FILES}"
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
EXAMPLE_RB.zip(EXAMPLE_DOC).each do |source, target|
|
109
|
-
file target => source do
|
110
|
-
open(source, "r") do |ins|
|
111
|
-
open(target, "w") do |outs|
|
112
|
-
outs.puts "= FlexMock Examples"
|
113
|
-
ins.each do |line|
|
114
|
-
outs.puts " #{line}"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
file "README.md" => ["Rakefile", "lib/flexmock/version.rb"] do
|
122
|
-
ruby %{-i.bak -pe '$_.sub!(/^Version: *((\\d+|beta|rc)\\.)+\\d+ *$/i, "Version :: #{PKG_VERSION}")' README.md} # "
|
123
|
-
end
|
124
|
-
|
125
|
-
desc "Fix the Darkfish CSS so that paragraphs in lists have a bit of spacing"
|
126
|
-
task :fixcss do
|
127
|
-
open("html/rdoc.css") do |ins|
|
128
|
-
open("html/rdoc.new", "w") do |outs|
|
129
|
-
count = 0
|
130
|
-
ins.each do |line|
|
131
|
-
if line =~ /^ *margin: +0;$/
|
132
|
-
count += 1
|
133
|
-
if count == 3
|
134
|
-
line = " margin: 0.5em 0;"
|
135
|
-
end
|
136
|
-
end
|
137
|
-
outs.puts line
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
rm_f "html/rdoc.css"
|
142
|
-
mv "html/rdoc.new", "html/rdoc.css"
|
143
|
-
end
|
144
|
-
|
145
|
-
# Package Task -------------------------------------------------------
|
146
|
-
|
147
|
-
if ! defined?(Gem)
|
148
|
-
puts "Package Target requires RubyGEMs"
|
149
|
-
else
|
150
|
-
spec = Gem::Specification.new do |s|
|
151
|
-
|
152
|
-
#### Basic information.
|
153
|
-
|
154
|
-
s.name = 'flexmock'
|
155
|
-
s.version = PKG_VERSION
|
156
|
-
s.summary = "Simple and Flexible Mock Objects for Testing"
|
157
|
-
s.description = %{
|
158
|
-
FlexMock is a extremely simple mock object class compatible
|
159
|
-
with the Test::Unit framework. Although the FlexMock's
|
160
|
-
interface is simple, it is very flexible.
|
161
|
-
} # '
|
162
|
-
|
163
|
-
#### Dependencies and requirements.
|
164
|
-
|
165
|
-
#s.add_dependency('log4r', '> 1.0.4')
|
166
|
-
#s.requirements << ""
|
167
|
-
|
168
|
-
#### Which files are to be included in this gem? Everything! (Except CVS directories.)
|
169
|
-
|
170
|
-
s.files = PKG_FILES.to_a
|
171
|
-
|
172
|
-
#### C code extensions.
|
173
|
-
|
174
|
-
#s.extensions << "ext/rmagic/extconf.rb"
|
175
|
-
|
176
|
-
#### Load-time details: library and application (you will need one or both).
|
177
|
-
|
178
|
-
s.require_path = 'lib' # Use these for libraries.
|
179
|
-
|
180
|
-
#### Documentation and testing.
|
181
|
-
|
182
|
-
s.has_rdoc = true
|
183
|
-
s.extra_rdoc_files = RDOC_FILES.reject { |fn| fn =~ /\.rb$/ }.to_a
|
184
|
-
s.rdoc_options <<
|
185
|
-
'--title' << 'FlexMock' <<
|
186
|
-
'--main' << 'README.rdoc' <<
|
187
|
-
'--line-numbers'
|
188
|
-
|
189
|
-
#### Author and project details.
|
190
|
-
|
191
|
-
s.author = "Jim Weirich"
|
192
|
-
s.email = "jim.weirich@gmail.com"
|
193
|
-
s.homepage = "https://github.com/jimweirich/flexmock"
|
194
|
-
end
|
195
|
-
|
196
|
-
Gem::PackageTask.new(spec) do |pkg|
|
197
|
-
pkg.need_zip = true
|
198
|
-
pkg.need_tar = false
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
require 'rake/contrib/publisher'
|
203
|
-
require 'rake/contrib/sshpublisher'
|
204
|
-
|
205
|
-
publisher = Rake::CompositePublisher.new
|
206
|
-
publisher.add(Rake::RubyForgePublisher.new('flexmock', 'jimweirich'))
|
207
|
-
|
208
|
-
desc "Publish the documentation on public websites"
|
209
|
-
task :publish => [:rdoc] do
|
210
|
-
publisher.upload
|
211
|
-
end
|
212
|
-
|
213
|
-
task :specs do
|
214
|
-
specs = FileList['test/spec_*.rb']
|
215
|
-
ENV['RUBYLIB'] = "lib:test:#{ENV['RUBYLIB']}"
|
216
|
-
sh %{rspec #{specs}}
|
217
|
-
end
|
218
|
-
|
219
|
-
task :tag do
|
220
|
-
sh "git tag 'flexmock-#{PKG_VERSION}'"
|
221
|
-
end
|
10
|
+
task :default => :test
|
@@ -0,0 +1,244 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.mock_with :flexmock
|
3
|
+
end
|
4
|
+
|
5
|
+
describe "Simple Spec" do
|
6
|
+
|
7
|
+
# Simple stubbing of some methods
|
8
|
+
|
9
|
+
it "stubs a couple of methods" do
|
10
|
+
m = flexmock(:pi => 3.1416, :e => 2.71)
|
11
|
+
m.pi.should == 3.1416
|
12
|
+
m.e.should == 2.71
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
describe "Returning Undefined" do
|
18
|
+
|
19
|
+
# Create a mock object that returns an undefined object for method calls
|
20
|
+
|
21
|
+
it "returns undefined values" do
|
22
|
+
m = flexmock("mock")
|
23
|
+
m.should_receive(:divide_by).with(0).
|
24
|
+
and_return_undefined
|
25
|
+
|
26
|
+
m.divide_by(0).should == FlexMock.undefined
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "Multiple Queries and Single Updates" do
|
31
|
+
|
32
|
+
# Expect multiple queries and a single update
|
33
|
+
|
34
|
+
# Multiple calls to the query method will be allows, and calls may
|
35
|
+
# have any argument list. Each call to query will return the three
|
36
|
+
# element array [1, 2, 3]. The call to update must have a specific
|
37
|
+
# argument of 5.
|
38
|
+
|
39
|
+
it "queries the db" do
|
40
|
+
db = flexmock('db')
|
41
|
+
db.should_receive(:query).and_return([1,2,3])
|
42
|
+
db.should_receive(:update).with(5).and_return(nil).once
|
43
|
+
|
44
|
+
# Test Code
|
45
|
+
|
46
|
+
db.query
|
47
|
+
db.update(5)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "Ordered Mocks" do
|
52
|
+
|
53
|
+
# Expect all queries before any updates
|
54
|
+
|
55
|
+
# All the query message must occur before any of the update
|
56
|
+
# messages.
|
57
|
+
|
58
|
+
it "queries and updates the database" do
|
59
|
+
db = flexmock('db')
|
60
|
+
db.should_receive(:query).and_return([1,2,3]).ordered
|
61
|
+
db.should_receive(:update).and_return(nil).ordered
|
62
|
+
|
63
|
+
# test code here
|
64
|
+
|
65
|
+
db.query
|
66
|
+
db.update
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "Ordered Mocks" do
|
71
|
+
|
72
|
+
# Expect several queries with different parameters
|
73
|
+
|
74
|
+
# The queries should happen after startup but before finish. The
|
75
|
+
# queries themselves may happen in any order (because they are in
|
76
|
+
# the same order group). The first two queries should happen exactly
|
77
|
+
# once, but the third query (which matches any query call with a
|
78
|
+
# four character parameter) may be called multiple times (but at
|
79
|
+
# least once). Startup and finish must also happen exactly once.
|
80
|
+
|
81
|
+
# Also note that we use the <code>with</code> method to match
|
82
|
+
# different argument values to figure out what value to return.
|
83
|
+
|
84
|
+
it "queries the database in a particular order" do
|
85
|
+
db = flexmock('db')
|
86
|
+
db.should_receive(:startup).once.ordered
|
87
|
+
db.should_receive(:query).with("CPWR").and_return(12.3).
|
88
|
+
once.ordered(:queries)
|
89
|
+
db.should_receive(:query).with("MSFT").and_return(10.0).
|
90
|
+
once.ordered(:queries)
|
91
|
+
db.should_receive(:query).with(/^....$/).and_return(3.3).
|
92
|
+
at_least.once.ordered(:queries)
|
93
|
+
db.should_receive(:finish).once.ordered
|
94
|
+
|
95
|
+
# Test Code
|
96
|
+
|
97
|
+
db.startup
|
98
|
+
db.query("MSFT")
|
99
|
+
db.query("XYZY")
|
100
|
+
db.query("CPWR")
|
101
|
+
db.finish
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "Ordered Mocks" do
|
106
|
+
|
107
|
+
# Same as above, but using the Record Mode interface
|
108
|
+
|
109
|
+
# The record mode interface offers much the same features as the
|
110
|
+
# <code>should_receive</code> interface introduced so far, but it
|
111
|
+
# allows the messages to be sent directly to a recording object
|
112
|
+
# rather than be specified indirectly using a symbol.
|
113
|
+
|
114
|
+
|
115
|
+
it "records the queries for replay" do
|
116
|
+
db = flexmock('db')
|
117
|
+
db.should_expect do |rec|
|
118
|
+
rec.startup.once.ordered
|
119
|
+
rec.query("CPWR") { 12.3 }.once.ordered(:queries)
|
120
|
+
rec.query("MSFT") { 10.0 }.once.ordered(:queries)
|
121
|
+
rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
|
122
|
+
rec.finish.once.ordered
|
123
|
+
end
|
124
|
+
|
125
|
+
# Test Code
|
126
|
+
|
127
|
+
db.startup
|
128
|
+
db.query("MSFT")
|
129
|
+
db.query("XYZY")
|
130
|
+
db.query("CPWR")
|
131
|
+
db.finish
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe "Record Mode" do
|
136
|
+
|
137
|
+
# Using Record Mode to record a known, good algorithm for testing
|
138
|
+
|
139
|
+
# Record mode is nice when you have a known, good algorithm that can
|
140
|
+
# use a recording mock object to record the steps. Then you compare
|
141
|
+
# the execution of a new algorithm to behavior of the old using the
|
142
|
+
# recorded expectations in the mock. For this you probably want to
|
143
|
+
# put the recorder in _strict_ mode so that the recorded
|
144
|
+
# expectations use exact matching on argument lists, and strict
|
145
|
+
# ordering of the method calls.
|
146
|
+
|
147
|
+
# <b>Note:</b> This is most useful when there are no queries on the
|
148
|
+
# mock objects, because the query responses cannot be programmed
|
149
|
+
# into the recorder object.
|
150
|
+
|
151
|
+
it "compares a know algorithm with a new algorithm" do
|
152
|
+
builder = flexmock('builder')
|
153
|
+
builder.should_expect do |rec|
|
154
|
+
rec.should_be_strict
|
155
|
+
known_good_way_to_build_xml(rec) # record the messages
|
156
|
+
end
|
157
|
+
new_way_to_build_xml(builder) # compare to new way
|
158
|
+
end
|
159
|
+
|
160
|
+
def known_good_way_to_build_xml(builder)
|
161
|
+
builder.person
|
162
|
+
end
|
163
|
+
|
164
|
+
def new_way_to_build_xml(builder)
|
165
|
+
builder.person
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "Multiple Return Values" do
|
171
|
+
|
172
|
+
# Expect multiple calls, returning a different value each time
|
173
|
+
|
174
|
+
# Sometimes you need to return different values for each call to a
|
175
|
+
# mocked method. This example shifts values out of a list for this
|
176
|
+
# effect.
|
177
|
+
|
178
|
+
it "returns multiple values" do
|
179
|
+
file = flexmock('file')
|
180
|
+
file.should_receive(:gets).with_no_args.
|
181
|
+
and_return("line 1\n", "line 2\n")
|
182
|
+
|
183
|
+
# test code here
|
184
|
+
|
185
|
+
file.gets # returns "line 1"
|
186
|
+
file.gets # returns "line 2"
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
describe "Ignore Unimportant Messages" do
|
191
|
+
|
192
|
+
# Ignore uninteresting messages
|
193
|
+
|
194
|
+
# Generally you need to mock only those methods that return an
|
195
|
+
# interesting value or wish to assert were sent in a particular
|
196
|
+
# manner. Use the <code>should_ignore_missing</code> method to turn
|
197
|
+
# on missing method ignoring.
|
198
|
+
|
199
|
+
it "ignores unimportant messages" do
|
200
|
+
m = flexmock('m')
|
201
|
+
m.should_receive(:an_important_message).and_return(1).once
|
202
|
+
m.should_ignore_missing
|
203
|
+
|
204
|
+
# Test Code
|
205
|
+
|
206
|
+
m.an_important_message
|
207
|
+
m.an_unimportant_message
|
208
|
+
end
|
209
|
+
|
210
|
+
# When <code>should_ignore_missing</code> is enabled, ignored
|
211
|
+
# missing methods will return an undefined object. Any operation on
|
212
|
+
# the undefined object will return the undefined object.
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
|
217
|
+
describe "Partial Mocks" do
|
218
|
+
|
219
|
+
# Mock just one method on an existing object
|
220
|
+
|
221
|
+
# The Portfolio class calculate the value of a set of stocks by
|
222
|
+
# talking to a quote service via a web service. Since we don't want
|
223
|
+
# to use a real web service in our unit tests, we will mock the
|
224
|
+
# quote service.
|
225
|
+
|
226
|
+
it "returns the portfolio value" do
|
227
|
+
flexmock(QuoteService).new_instances do |m|
|
228
|
+
m.should_receive(:quote).and_return(100)
|
229
|
+
end
|
230
|
+
port = Portfolio.new
|
231
|
+
value = port.value # Portfolio calls QuoteService.quote
|
232
|
+
value.should == 100
|
233
|
+
end
|
234
|
+
|
235
|
+
class QuoteService
|
236
|
+
end
|
237
|
+
|
238
|
+
class Portfolio
|
239
|
+
def value
|
240
|
+
qs = QuoteService.new
|
241
|
+
qs.quote
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|