/
README
136 lines (89 loc) · 3.52 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
= Amok, a compact mock library
Amok is a simple and compact mock library that is independent of any
testing framework but made to work nicely with test/spec and bacon.
It provides mocking and stubbing of any object's methods.
== Whirl-wind tour
Amok.new(Item) {
on.find(1) {
Amok.with(:name => "Deschutes",
:description => "Deschutes model Guitar",
:unit_price => Money.new(2400.00))
}
}
Item.find(1).name.should.equal "Deschutes"
Amok.with(Google4R::Checkout::CheckoutCommand) { |obj, mock|
mock.on.new { |*args|
Amok.new(Google4R::Checkout::CheckoutCommand.new(*args)) {
need(1).send_to_google_checkout {
Amok.with(:redirect_url => "http://google.response.url")
}
}
}
Item.find(1).purchase.redirect_url.should.equal "http://google.response.url"
}
== Implemented features
Amok.new(obj)::
creates a mock definition object to stub methods of obj.
Amok.with(obj) { |obj, mock| ... }::
Use Amok.with to automatically check at the end of the block whether
required methods have been called.
Amok.make(hash)::
is used to conveniently make mocks that return the hash values when
the hash key is sent.
mock.on._method_ {...}::
run the block when _method_ is sent. The block can call
+mock.previous(_method_, ...)+ to call the original method.
mock.on._method_(_args_...) {...}::
run the block when _method_ is sent with exactly the same _args_,
else run the original method. The block can call
+mock.previous(_method_, ...)+ to call the original method.
mock.on(method, args) {...}::
like mock.on._method_, but _method_ can be passed as a symbol and _args_.
(This allows you to define methods that require empty _args_,
because mock.on._method_() does not check _args_.)
mock.need._method_::
check that _method_ is called
mock.need(n)._method_::
check that _method_ is called exactly _n_ times.
mock.need._method_ {...}::
like mock.on._method_ {...}, but check that _method_ is called.
mock.need(n)._method_ {...}::
like mock.on._method_ {...}, but check that _method_ is called exactly
_n_ times.
mock.need(method, args, n) {...}::
same as mock.need(n)._method_(args).
mock.never._method_::
check that _method_ is never called.
mock.never(method)::
same as mock.never._method_.
mock.successful?::
returns true if all checks passed.
mock.errors::
returns an array of failed checks.
mock.validate::
raises Amok::Failed when checks failed.
== Words of advice
Mocking and stubbing are possibly dangerous operations, and easily can
change the meaning of your tests in ways you don't intent. Be alert,
and only use them when needed. Remember, the purpose of your tests is
to test your code, and not test your mocks.
== Thanks to
* raggi and jazen for making me write it.
* mfp for instance_exec.
* authors of all previous mock libraries for inspiration.
== History
* September 25th, 2008: Coding started.
== Contact
Please mail bugs, suggestions and patches to
<mailto:chneukirchen@gmail.com>.
Git repository (rebased patches on master are most welcome):
git://github.com/chneukirchen/amok.git
Project page:
http://github.com/chneukirchen/amok/tree/master
== Copying
Copyright (C) 2008 Christian Neukirchen <http://purl.org/net/chneukirchen>
Amok is freely distributable under the terms of an MIT-style license.
See COPYING or http://www.opensource.org/licenses/mit-license.php.
== Links
Mocks Aren't Stubs:: <http://martinfowler.com/articles/mocksArentStubs.html>
Christian Neukirchen:: <http://chneukirchen.org/>