| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 |
<HTML> |
| 3 |
<HEAD> |
| 4 |
<META NAME="generator" CONTENT="http://txt2tags.sf.net"> |
| 5 |
<LINK REL="stylesheet" TYPE="text/css" HREF="style.css"> |
| 6 |
<TITLE>LeMock</TITLE> |
| 7 |
</HEAD> |
| 8 |
<BODY> |
| 9 |
|
| 10 |
<DIV CLASS="header" ID="header"> |
| 11 |
<H1>LeMock</H1> |
| 12 |
</DIV> |
| 13 |
|
| 14 |
<DIV CLASS="body" ID="page-userguide"> |
| 15 |
<ul id="main_menu"> |
| 16 |
<li id="main_menu-README"><a href="README.html" >Readme</a></li> |
| 17 |
<li id="main_menu-COPYRIGHT"><a href="COPYRIGHT.html">License</a></li> |
| 18 |
<li id="main_menu-userguide"><a href="userguide.html">Userguide</a></li> |
| 19 |
<li id="main_menu-HISTORY"><a href="HISTORY.html" >History</a></li> |
| 20 |
<li id="main_menu-DEVEL"><a href="DEVEL.html" >Devel</a></li> |
| 21 |
</ul> |
| 22 |
<DIV CLASS="toc" ID="toc"> |
| 23 |
<UL> |
| 24 |
<LI><A HREF="#toc1">Introduction</A> |
| 25 |
<LI><A HREF="#toc2">The Mock Object</A> |
| 26 |
<UL> |
| 27 |
<LI><A HREF="#toc3">Actions</A> |
| 28 |
<LI><A HREF="#anyargs">Anyargs</A> |
| 29 |
</UL> |
| 30 |
<LI><A HREF="#toc5">The Controller</A> |
| 31 |
<UL> |
| 32 |
<LI><A HREF="#toc6">Returns & Error</A> |
| 33 |
<LI><A HREF="#toc7">Label & Depend</A> |
| 34 |
<LI><A HREF="#toc8">Times</A> |
| 35 |
<LI><A HREF="#toc9">Close</A> |
| 36 |
</UL> |
| 37 |
<LI><A HREF="#toc10">Tricks</A> |
| 38 |
<UL> |
| 39 |
<LI><A HREF="#toc11">Method Overloading</A> |
| 40 |
</UL> |
| 41 |
</UL> |
| 42 |
|
| 43 |
</DIV> |
| 44 |
<A NAME="toc1"></A> |
| 45 |
<H1>Introduction</H1> |
| 46 |
<P> |
| 47 |
Mock objects replace difficult external objects during unit testing by |
| 48 |
simulating the behaviors of the replaced objects. This is done by first |
| 49 |
recording actions and their responses with the mock objects, and then |
| 50 |
switching to replay mode. During replay mode the mock objects simulate the |
| 51 |
replaced objects by looking up actions and replaying the recorded |
| 52 |
responses, and finally verifying that all expected actions where completely |
| 53 |
replayed. |
| 54 |
</P> |
| 55 |
<P> |
| 56 |
Actions are stored in a list in a special controller object. During replay |
| 57 |
the list is searched in recording order for the first matching action that |
| 58 |
can be replayed. |
| 59 |
</P> |
| 60 |
<P> |
| 61 |
Restrictions on the actions can be inserted during the recording phase. An |
| 62 |
action can have a maximum count of how many times it will be replayed, and |
| 63 |
a minimum count of how many times it must be replayed to be satisfied. An |
| 64 |
action can depend on any set of other actions, and can not be replayed |
| 65 |
before all of its depended actions are satisfied. An action can close any |
| 66 |
set of actions when it is replayed, which stops all further replaying of |
| 67 |
the closed actions. This is good for simulating state changes. |
| 68 |
</P> |
| 69 |
<H3>Example</H3> |
| 70 |
<P> |
| 71 |
This example tests that the insert_data function of the foo module handles |
| 72 |
a missing data base table gracefully. |
| 73 |
</P> |
| 74 |
<PRE> |
| 75 |
-- Setup |
| 76 |
require 'lemock' |
| 77 |
local mc = lemock.controller() |
| 78 |
local sqlite3 = mc:mock() |
| 79 |
local env = mc:mock() |
| 80 |
local con = mc:mock() |
| 81 |
package.loaded.luasql = nil |
| 82 |
package.preload['luasql.sqlite3'] = function () |
| 83 |
luasql = {} |
| 84 |
luasql.sqlite3 = sqlite3 |
| 85 |
return sqlite3 |
| 86 |
end |
| 87 |
|
| 88 |
-- Record |
| 89 |
sqlite3() ;mc :returns(env) |
| 90 |
env:connect('/data/base') ;mc :returns(con) |
| 91 |
con:execute(mc.ANYARGS) ;mc :error('LuaSQL: no such table') |
| 92 |
con:close() |
| 93 |
env:close() |
| 94 |
|
| 95 |
-- Replay |
| 96 |
mc:replay() |
| 97 |
require 'foo' |
| 98 |
local res = foo.insert_data(17) |
| 99 |
assert(res==false) |
| 100 |
|
| 101 |
--Verify |
| 102 |
mc:verify() |
| 103 |
</PRE> |
| 104 |
<P></P> |
| 105 |
<P> |
| 106 |
First a controller is created. Then three mock objects are created, one for |
| 107 |
the sqlite3 module, and two for objects returned by the (simulated) module. |
| 108 |
</P> |
| 109 |
<P> |
| 110 |
Then a preloader for the sqlite3 module is installed, which returns the |
| 111 |
sqlite3 mock object instead of the actual sqlite3 module. |
| 112 |
</P> |
| 113 |
<P> |
| 114 |
In the record phase the expected calls and their return values (or thrown |
| 115 |
errors) are recorded. The order is not significant, so this simplified test |
| 116 |
will not detect if the close method is called before the execute method. |
| 117 |
</P> |
| 118 |
<P> |
| 119 |
In the replay phase the tested module is loaded and executed. It will use |
| 120 |
the mock objects instead of the real data base, and if it makes any |
| 121 |
unrecorded calls, an error is thrown. |
| 122 |
</P> |
| 123 |
<P> |
| 124 |
The verify phase asserts that all recorded actions have been replayed. If |
| 125 |
the foo module for example forgets to call the close method, verify throws |
| 126 |
an error. |
| 127 |
</P> |
| 128 |
<A NAME="toc2"></A> |
| 129 |
<H1>The Mock Object</H1> |
| 130 |
<P> |
| 131 |
Mock objects are empty objects with special Lua meta methods that detect |
| 132 |
actions performed with the object. What happens depends on the state |
| 133 |
(recording or replaying) of the controller which created the mock object. |
| 134 |
During recording the mock object adds the action to the controller's list |
| 135 |
of recorded actions. During replay the mock object looks for a matching |
| 136 |
recorded action that can be replayed, and simulates the action. |
| 137 |
</P> |
| 138 |
<P> |
| 139 |
Some action attributes can not be inferred by the mock objects, for example |
| 140 |
return values. These attributes have to be added afterwards with special |
| 141 |
controller methods, and always affect the last recorded action. |
| 142 |
</P> |
| 143 |
<A NAME="toc3"></A> |
| 144 |
<H2>Actions</H2> |
| 145 |
<P> |
| 146 |
Mock objects detect four types of actions: assignment, indexing, method |
| 147 |
call, and self call. During replay an action will only match if it is the |
| 148 |
very same action, that is, the same type of action performed on the same |
| 149 |
mock object with all the same arguments. There are however |
| 150 |
<A HREF="#anyargs">special arguments</A> that can be used during recording. |
| 151 |
</P> |
| 152 |
<PRE> |
| 153 |
require 'lemock' |
| 154 |
local mc = lemock.controller() |
| 155 |
local m = mc:mock() |
| 156 |
|
| 157 |
m.x = 17 -- assignment |
| 158 |
r = m.x -- indexing |
| 159 |
m.x(1,2,3) -- method call |
| 160 |
m:x(1,2,3) -- method call |
| 161 |
m(1,2,3) -- self call |
| 162 |
</PRE> |
| 163 |
<A NAME="anyargs"></A> |
| 164 |
<H2>Anyargs</H2> |
| 165 |
<P> |
| 166 |
An <I>anyarg</I> is a special argument used when recording, that will match |
| 167 |
any argument during replay. It can appear anywhere and any times in an |
| 168 |
argument list, or as the argument in an assignment, to replace real |
| 169 |
arguments. There is also <I>anyargs</I>, which will match any number |
| 170 |
(including zero) of any arguments. Anyargs can only appear as the last |
| 171 |
argument of an argument list. Anyarg and anyargs are handy when the actual |
| 172 |
values of the arguments during replay are unimportant or unknown. |
| 173 |
</P> |
| 174 |
<P> |
| 175 |
Anyarg and anyargs are constants defined in the controller object. |
| 176 |
</P> |
| 177 |
<H3>Example</H3> |
| 178 |
<P> |
| 179 |
This example tests that the fetch_data function of module foo waits a while |
| 180 |
and retries when no data is immediately available, and that it updates the |
| 181 |
value of lasttime. |
| 182 |
</P> |
| 183 |
<PRE> |
| 184 |
require 'lemock' |
| 185 |
local mc = lemock.controller() |
| 186 |
local con = mc:mock() |
| 187 |
|
| 188 |
con:poll() ;mc :returns(nil) |
| 189 |
con:sleep(mc.ANYARG) |
| 190 |
con:poll() ;mc :returns('123.45') |
| 191 |
con.lasttime = mc.ANYARG |
| 192 |
|
| 193 |
mc:replay() |
| 194 |
require 'foo' |
| 195 |
local res = foo.fetch_data(con) |
| 196 |
assert( math.abs(res-123.45) < 0.0005 ) |
| 197 |
|
| 198 |
mc:verify() |
| 199 |
</PRE> |
| 200 |
<A NAME="toc5"></A> |
| 201 |
<H1>The Controller</H1> |
| 202 |
<P> |
| 203 |
The controller's main purpose is to store the recorded actions, create mock |
| 204 |
objects, switch to replay mode, and verify the completion of the replay |
| 205 |
phase. But it is also needed to set or change special action attributes |
| 206 |
during recording. |
| 207 |
</P> |
| 208 |
<P> |
| 209 |
It is possible, although doubtfully useful, to use several controllers in |
| 210 |
parallel during a single unit test. Each controller maintains its own |
| 211 |
action list and state, and mock objects remember which controller they |
| 212 |
belong to. |
| 213 |
</P> |
| 214 |
<A NAME="toc6"></A> |
| 215 |
<H2>Returns & Error</H2> |
| 216 |
<P> |
| 217 |
The by far most useful special action attribute is the return value. |
| 218 |
Indexing actions can return a single value, while call actions and self |
| 219 |
call actions can return a list of values. The return value is set with the |
| 220 |
<I>returns</I> method, and it is an error to set the return value twice for |
| 221 |
the same action. |
| 222 |
</P> |
| 223 |
<P> |
| 224 |
For purposes of unit testing it is often useful to simulate errors. All |
| 225 |
actions can raise an error, and return an error value (usually a string). |
| 226 |
The return value is set with the <I>error</I> method. An action can not have |
| 227 |
both a return value and raise an error. |
| 228 |
</P> |
| 229 |
<H3>Example</H3> |
| 230 |
<PRE> |
| 231 |
require 'lemock' |
| 232 |
local mc = lemock.controller() |
| 233 |
local m = mc:mock() |
| 234 |
|
| 235 |
m:foo(17) ;mc :returns(nil, "index out of range") |
| 236 |
m:bar(-1) ;mc :error("invalid index") |
| 237 |
</PRE> |
| 238 |
<A NAME="toc7"></A> |
| 239 |
<H2>Label & Depend</H2> |
| 240 |
<P> |
| 241 |
Dependencies block actions from replaying until other actions have replayed |
| 242 |
first. They can be used to verify that actions are being replayed in a |
| 243 |
valid order. |
| 244 |
</P> |
| 245 |
<P> |
| 246 |
To add dependencies, actions must first be labeled with one or more |
| 247 |
<I>labels</I>. The same label can be given to several actions. As long as some |
| 248 |
action with the label remains unsatisfied, that label is blocked, and all |
| 249 |
actions depending on that label will not replay. |
| 250 |
</P> |
| 251 |
<H3>Example</H3> |
| 252 |
<P> |
| 253 |
This (contrived) example tests that function draw_square in module foo |
| 254 |
calls all the necessary drawing methods of a square object in a correct |
| 255 |
order. Note that there can be more than one correct order. |
| 256 |
</P> |
| 257 |
<PRE> |
| 258 |
require 'lemock' |
| 259 |
local mc = lemock.controller() |
| 260 |
local square = mc:mock() |
| 261 |
|
| 262 |
square:topleft() ;mc :label('tl') |
| 263 |
square:topright() ;mc :label('tr') |
| 264 |
square:botleft() ;mc :label('bl') |
| 265 |
square:botright() ;mc :label('br') |
| 266 |
square:leftedge() ;mc :label('edge') :depend('tl', 'bl') |
| 267 |
square:rightedge() ;mc :label('edge') :depend('tr', 'br') |
| 268 |
square:topedge() ;mc :label('edge') :depend('tl', 'tr') |
| 269 |
square:botedge() ;mc :label('edge') :depend('bl', 'br') |
| 270 |
square:fill() ;mc :depend('edge') |
| 271 |
|
| 272 |
mc:replay() |
| 273 |
require 'foo' |
| 274 |
foo.draw_square( square ) |
| 275 |
|
| 276 |
mc:verify() |
| 277 |
</PRE> |
| 278 |
<P></P> |
| 279 |
<P> |
| 280 |
This example demonstrates two different ways of using dependencies. All the |
| 281 |
corners have unique labels, because each edge depend on a set of specific |
| 282 |
corners. But all the edges have the same label, because the fill operation |
| 283 |
only depends on <I>all</I> edges have been satisfied. |
| 284 |
</P> |
| 285 |
<A NAME="toc8"></A> |
| 286 |
<H2>Times</H2> |
| 287 |
<P> |
| 288 |
The default for a recorded action is to be replayed exactly once. |
| 289 |
<CODE>times(2)</CODE> changes that to exactly two times, and <CODE>times(1,2)</CODE> changes |
| 290 |
it to at least one time and at most two times. |
| 291 |
</P> |
| 292 |
<P> |
| 293 |
When the action has been replayed the least count times it is |
| 294 |
<I>satisfied</I>, which means verify will not complain about it, and it no |
| 295 |
longer blocks actions that depend on this action from being replayed. If |
| 296 |
the least count is zero the action is automatically satisfied and need not |
| 297 |
be replayed at all, i.e., it is optional. |
| 298 |
</P> |
| 299 |
<P> |
| 300 |
When the action has been replayed the most count times it will not replay |
| 301 |
any more. The most replay count can be set to infinity (<CODE>math.huge</CODE> or |
| 302 |
<CODE>1/0</CODE>), in which case the action will never stop replaying. |
| 303 |
</P> |
| 304 |
<P> |
| 305 |
<CODE>anytimes()</CODE> can be used as an alias for <CODE>times(0,1/0)</CODE>, and |
| 306 |
<CODE>atleastonce()</CODE> can be used as an alias for <CODE>times(1,1/0)</CODE>. |
| 307 |
</P> |
| 308 |
<H3>Example</H3> |
| 309 |
<P> |
| 310 |
This example tests that method update is called at least once. |
| 311 |
</P> |
| 312 |
<PRE> |
| 313 |
require 'lemock' |
| 314 |
local mc = lemock.controller() |
| 315 |
local con = mc:mock() |
| 316 |
|
| 317 |
con:log(mc.ANYARGS) ;mc :anytimes() |
| 318 |
con:update('x',3) ;mc :returns(true) :atleastonce() |
| 319 |
|
| 320 |
mc:replay() |
| 321 |
require 'foo' |
| 322 |
local watcher = foo.mk_watcher( con ) |
| 323 |
watcher:set( 'x', 3 ) |
| 324 |
|
| 325 |
mc:verify() |
| 326 |
</PRE> |
| 327 |
<A NAME="toc9"></A> |
| 328 |
<H2>Close</H2> |
| 329 |
<P> |
| 330 |
Close can be used to simulate state changes in a limited way. When an |
| 331 |
action with a close statement is replayed for the first time, it will |
| 332 |
permanently block all labels in its close statement, so that actions with |
| 333 |
these labels no longer replays. This passes on matching to later actions in |
| 334 |
the action list, which may for example have different return values. |
| 335 |
</P> |
| 336 |
<P> |
| 337 |
The closing simply blocks the labels, and it has nothing to do with max |
| 338 |
replay counts or if closed actions have been satisfied or not. Closing an |
| 339 |
unsatisfied action however results in an immediate failure. |
| 340 |
</P> |
| 341 |
<H3>Example</H3> |
| 342 |
<P> |
| 343 |
This example tests that the dump function of module foo calls the myio |
| 344 |
functions in a correct order. The read function can be called any number of |
| 345 |
times, until it is closed by the close function. |
| 346 |
</P> |
| 347 |
<PRE> |
| 348 |
require 'lemock' |
| 349 |
local mc = lemock.controller() |
| 350 |
local myio = mc:mock() |
| 351 |
local fs = mc:mock() |
| 352 |
|
| 353 |
myio.open('abc', 'r') ;mc :returns(fs) |
| 354 |
mc :label('open') |
| 355 |
|
| 356 |
fs:read(mc.ANYARG) ;mc :returns('data') |
| 357 |
mc :atleastonce() :label('read') :depend('open') |
| 358 |
|
| 359 |
fs:close() ;mc :returns(true) |
| 360 |
mc :depend('open') :close('read') |
| 361 |
|
| 362 |
mc:replay() |
| 363 |
require 'foo' |
| 364 |
foo.dump(myio, 'abc', 128) |
| 365 |
|
| 366 |
mc:verify() |
| 367 |
</PRE> |
| 368 |
<A NAME="toc10"></A> |
| 369 |
<H1>Tricks</H1> |
| 370 |
<P> |
| 371 |
Mock objects are completely empty, and do not contain any methods or |
| 372 |
properties of their own. If they did, that would risk shadowing a name of a |
| 373 |
simulated object's method or property. There is however nothing preventing |
| 374 |
users from defining methods and properties in mock objects. This way mock |
| 375 |
objects can be turned into stubs, or a kind of mock–stub hybrid. |
| 376 |
</P> |
| 377 |
<A NAME="toc11"></A> |
| 378 |
<H2>Method Overloading</H2> |
| 379 |
<P> |
| 380 |
Lua does not support method overloading, but it can be (and sometimes is) |
| 381 |
implemented manually by testing of function arguments. This presents a |
| 382 |
problem to LeMock, because it matches exact arguments, and anyargs in not |
| 383 |
sufficient. In this case the mock object can be extended with a dispatcher |
| 384 |
function. |
| 385 |
</P> |
| 386 |
<H3>Example</H3> |
| 387 |
<P> |
| 388 |
This example shows a mock object with an overloaded add function. The stub |
| 389 |
function can not be defined in the usual way, because that would record an |
| 390 |
assignment action; it needs to be defined with <I>rawset</I>. |
| 391 |
</P> |
| 392 |
<PRE> |
| 393 |
require 'lemock' |
| 394 |
local mc = lemock.controller() |
| 395 |
local m = mc:mock() |
| 396 |
|
| 397 |
do |
| 398 |
local function add (a, b) |
| 399 |
if type(a) == 'number' then |
| 400 |
return m.add_number(a, b) |
| 401 |
else |
| 402 |
return m.add_string(a, b) |
| 403 |
end |
| 404 |
end |
| 405 |
rawset( m, 'add', add ) -- not recorded |
| 406 |
end -- do |
| 407 |
|
| 408 |
m.add_number(1, 2) ;mc :returns(3) |
| 409 |
m.add_string('foo', 'bar') ;mc :returns('foobar') |
| 410 |
|
| 411 |
mc:replay() |
| 412 |
assert_equal( 3, m.add(1, 2) ) |
| 413 |
assert_equal( 'foobar', m.add('foo', 'bar') ) |
| 414 |
|
| 415 |
mc:verify() |
| 416 |
</PRE> |
| 417 |
<HR NOSHADE SIZE=1> |
| 418 |
<P> |
| 419 |
2009-05-31 |
| 420 |
</P> |
| 421 |
</DIV> |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
</BODY></HTML> |