rspec 0.5.11 → 0.5.12
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/CHANGES +12 -0
- data/README +4 -2
- data/Rakefile +14 -4
- data/bin/spec +7 -7
- data/bin/test2spec +13 -8
- data/lib/spec/api/helper/diff.rb +53 -0
- data/lib/spec/api/helper/instance_helper.rb +11 -11
- data/lib/spec/api/helper/instance_negator.rb +11 -11
- data/lib/spec/api/helper/kind_helper.rb +11 -11
- data/lib/spec/api/helper/kind_negator.rb +11 -11
- data/lib/spec/api/helper/respond_helper.rb +11 -11
- data/lib/spec/api/helper/respond_negator.rb +11 -11
- data/lib/spec/api/helper/should_base.rb +6 -6
- data/lib/spec/api/helper/should_helper.rb +35 -35
- data/lib/spec/api/helper/should_negator.rb +20 -20
- data/lib/spec/rake/spectask.rb +15 -6
- data/lib/spec/runner.rb +1 -0
- data/lib/spec/runner/backtrace_tweaker.rb +2 -0
- data/lib/spec/runner/base_text_formatter.rb +3 -1
- data/lib/spec/runner/html_formatter.rb +153 -0
- data/lib/spec/runner/option_parser.rb +9 -2
- data/lib/spec/runner/progress_bar_formatter.rb +4 -0
- data/lib/spec/runner/rdoc_formatter.rb +3 -0
- data/lib/spec/runner/reporter.rb +5 -5
- data/lib/spec/runner/specdoc_formatter.rb +3 -0
- data/lib/spec/test_to_spec/sexp_transformer.rb +1 -0
- data/lib/spec/test_to_spec/translation_test_runner.rb +10 -2
- data/lib/spec/version.rb +1 -1
- data/test/spec/api/helper/arbitrary_predicate_test.rb +49 -73
- data/test/spec/api/helper/diff_test.rb +60 -0
- data/test/spec/api/helper/equality_test.rb +14 -0
- data/test/spec/api/helper/should_have_test.rb +0 -29
- data/test/spec/api/helper/typing_test.rb +87 -87
- data/test/spec/api/sugar_test.rb +0 -6
- data/test/spec/runner/backtrace_tweaker_test.rb +8 -2
- data/test/spec/runner/html_formatter_test.rb +47 -0
- data/test/spec/runner/option_parser_test.rb +0 -5
- data/test/test_classes.rb +73 -0
- data/test/test_helper.rb +3 -0
- metadata +8 -49
- data/doc/README +0 -3
- data/doc/config.yaml +0 -2
- data/doc/plugin/syntax.rb +0 -60
- data/doc/plugin/version.rb +0 -19
- data/doc/src/core_team.page +0 -31
- data/doc/src/default.css +0 -199
- data/doc/src/default.template +0 -31
- data/doc/src/documentation/index.page +0 -188
- data/doc/src/documentation/meta.info +0 -22
- data/doc/src/documentation/mocks.page +0 -287
- data/doc/src/documentation/underscores.page +0 -21
- data/doc/src/examples.page +0 -9
- data/doc/src/images/David_and_Aslak.jpg +0 -0
- data/doc/src/images/Whats_That_Dude.jpg +0 -0
- data/doc/src/images/ducks1.png +0 -0
- data/doc/src/images/ul.gif +0 -0
- data/doc/src/index.page +0 -74
- data/doc/src/meta.info +0 -33
- data/doc/src/tools/index.page +0 -49
- data/doc/src/tools/meta.info +0 -18
- data/doc/src/tools/rails.page +0 -132
- data/doc/src/tools/rake.page +0 -21
- data/doc/src/tools/rcov.page +0 -28
- data/doc/src/tools/spec.page +0 -129
- data/doc/src/tools/test2spec.page +0 -103
- data/doc/src/tutorials/index.page +0 -52
- data/doc/src/tutorials/meta.info +0 -36
- data/doc/src/tutorials/notes.txt +0 -263
- data/doc/src/tutorials/stack.rb +0 -11
- data/doc/src/tutorials/stack_01.page +0 -226
- data/doc/src/tutorials/stack_02.page +0 -182
- data/doc/src/tutorials/stack_03.page +0 -283
- data/doc/src/tutorials/stack_04.page +0 -164
- data/doc/src/tutorials/stack_04.page.orig +0 -123
- data/doc/src/tutorials/stack_05.page +0 -90
- data/doc/src/tutorials/stack_05.page.orig +0 -124
- data/doc/src/tutorials/stack_06.page +0 -359
- data/doc/src/tutorials/stack_06.page.orig +0 -359
- data/doc/src/tutorials/stack_spec.rb +0 -25
- data/doc/src/ul.gif +0 -0
data/doc/src/default.template
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
3
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{lang:}">
|
4
|
-
<head>
|
5
|
-
<title>{title: }</title>
|
6
|
-
<link href="{relocatable: default.css}" rel="stylesheet" type="text/css" media="screen"/>
|
7
|
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
8
|
-
</head>
|
9
|
-
<body>
|
10
|
-
|
11
|
-
<div id="container">
|
12
|
-
<div id="header">
|
13
|
-
<h2>Behaviour Driven Development for <strong>Ruby</strong></h2>
|
14
|
-
<h1>RSpec {version: }</h1>
|
15
|
-
</div>
|
16
|
-
|
17
|
-
<div id="navigation">
|
18
|
-
{menu: {menuStyle: horizontal}}
|
19
|
-
</div>
|
20
|
-
|
21
|
-
<div id="content">
|
22
|
-
{content: }
|
23
|
-
|
24
|
-
<div id="footer">
|
25
|
-
Webdesign by <a href="http://www.oswd.org/user/profile/id/9462">JM</a>
|
26
|
-
</div>
|
27
|
-
</div>
|
28
|
-
</div>
|
29
|
-
|
30
|
-
</body>
|
31
|
-
</html>
|
@@ -1,188 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Core API
|
3
|
-
inMenu: true
|
4
|
-
ordering: 5
|
5
|
-
---
|
6
|
-
h2. Core API
|
7
|
-
|
8
|
-
When RSpec executes specifications, it defines a method <code>should</code> on every object in the system. This <code>should</code> method is your entry to the magic of RSpec.
|
9
|
-
|
10
|
-
Almost all expectation forms have a corresponding negated form. It is listed when it is supported and, unless otherwise stated, is met when ever the non-negated form would be violated.
|
11
|
-
|
12
|
-
h3. General
|
13
|
-
|
14
|
-
h4. Arbitrary Block
|
15
|
-
|
16
|
-
<ruby>
|
17
|
-
target.should_satisfy {|arg| ...}
|
18
|
-
target.should_not_satisfy {|arg| ...}
|
19
|
-
</ruby>
|
20
|
-
|
21
|
-
The supplied block is evaluated, passing <code>target</code> as the sole argument. If the block evaluates to <code>false</code>, <code>ExpectationNotMetError</code> is raised.
|
22
|
-
|
23
|
-
<ruby>
|
24
|
-
target.should_satisfy {|arg| arg > 0}
|
25
|
-
</ruby>
|
26
|
-
|
27
|
-
h4. Equality
|
28
|
-
|
29
|
-
<ruby>
|
30
|
-
target.should_equal <value>
|
31
|
-
target.should_not_equal <value>
|
32
|
-
</ruby>
|
33
|
-
|
34
|
-
The target object is compared to <code>value</code> using ==. If the result is <code>false</code>, <code>ExpectationNotMetError</code> is raised.
|
35
|
-
|
36
|
-
h4. Floating Point Comparison
|
37
|
-
|
38
|
-
<ruby>
|
39
|
-
target.should_be_close <value>, <tolerance>
|
40
|
-
target.should_not_be_close <value>, <tolerance>
|
41
|
-
</ruby>
|
42
|
-
|
43
|
-
The target object is compared to <code>value</code>. If they differ by more that <code>tolerance</code>, <code>ExpectationNotMetError</code> is raised. In the negated case, <code>ExpectationNotMetError</code> is raised if they differ by less than <code>tolerance</code>.
|
44
|
-
|
45
|
-
<ruby>
|
46
|
-
target.should_be_close 27.35, 0.05
|
47
|
-
</ruby>
|
48
|
-
|
49
|
-
h4. Identity
|
50
|
-
|
51
|
-
<ruby>
|
52
|
-
target.should_be <value>
|
53
|
-
target.should_not_be <value>
|
54
|
-
</ruby>
|
55
|
-
|
56
|
-
The target object is compared to <code>value</code> using <code>equal?</code>. If the result is <code>false</code>, <code>ExpectationNotMetError</code> is raised.
|
57
|
-
|
58
|
-
h4. Arbitrary Predicate
|
59
|
-
|
60
|
-
<ruby>
|
61
|
-
target.should_predicate [optional args]
|
62
|
-
target.should_be_predicate [optional args]
|
63
|
-
target.should_not_predicate [optional args]
|
64
|
-
target.should_not_be_predicate [optional args]
|
65
|
-
</ruby>
|
66
|
-
|
67
|
-
The message <code>predicate?</code> is sent to <code>target</code> with any supplied arguments. If the result is <code>false</code>, <code>ExpectationNotMetError</code> is raised.
|
68
|
-
|
69
|
-
For example:
|
70
|
-
|
71
|
-
<ruby>
|
72
|
-
container.should_include('a') => container.include?('a')
|
73
|
-
container.should_be_empty => container.empty?
|
74
|
-
</ruby>
|
75
|
-
|
76
|
-
h4. Pattern Matching
|
77
|
-
|
78
|
-
<ruby>
|
79
|
-
target.should_match <regex>
|
80
|
-
target.should_not_match <regex>
|
81
|
-
</ruby>
|
82
|
-
|
83
|
-
The <code>target</code> is matched against <code>regex</code>. An <code>ExpectationNotMetError</code> is raised if the match fails.
|
84
|
-
|
85
|
-
h3. Class/Type
|
86
|
-
|
87
|
-
h4. Direct Instance
|
88
|
-
|
89
|
-
<ruby>
|
90
|
-
target.should_be_an_instance_of <class>
|
91
|
-
target.should_not_be_an_instance_of <class>
|
92
|
-
</ruby>
|
93
|
-
|
94
|
-
An <code>ExpectationNotMetError</code> is raised if <code>target</code> is not or is, respectively, an direct instance of <code>class</code>. As expected this correlates to <code>target.instance_of? class</code>.
|
95
|
-
|
96
|
-
h4. Ancestor Class
|
97
|
-
|
98
|
-
<ruby>
|
99
|
-
target.should_be_a_kind_of <class>
|
100
|
-
target.should_not_be_a_kind_of <class>
|
101
|
-
</ruby>
|
102
|
-
|
103
|
-
As above, but uses <code>target.kind_of? class</code>: checking whether <code>class</code> is the direct class of <code>target</code>, or an ancestor of <code>target</code>'s direct class.
|
104
|
-
|
105
|
-
h4. Type
|
106
|
-
|
107
|
-
<ruby>
|
108
|
-
target.should_respond_to <symbol>
|
109
|
-
target.should_not_respond_to <symbol>
|
110
|
-
</ruby>
|
111
|
-
|
112
|
-
Uses <code>target.respond_to?(symbol)</code> to check whether <code>symbol</code> is the name of a message that <code>target</code> understands.
|
113
|
-
|
114
|
-
h3. Procs
|
115
|
-
|
116
|
-
h4. Raising
|
117
|
-
|
118
|
-
<ruby>
|
119
|
-
proc.should_raise <exception>
|
120
|
-
proc.should_not_raise <exception>
|
121
|
-
</ruby>
|
122
|
-
|
123
|
-
Checks that <code>proc</code> causes the named exception to be raised or not. The latter is actually one of two cases: some other exception is raised, or no exception is raised. Typically the <code>proc</code> is created in place using <code>lambda</code>. For example:
|
124
|
-
|
125
|
-
<ruby>
|
126
|
-
lambda { 3 / 0 }.should_raise ZeroDivisionError
|
127
|
-
</ruby>
|
128
|
-
|
129
|
-
There is a more general form as well.
|
130
|
-
|
131
|
-
<ruby>
|
132
|
-
proc.should_raise
|
133
|
-
proc.should_not_raise
|
134
|
-
</ruby>
|
135
|
-
|
136
|
-
These forms don't worry about what exception is raised (or not). All they are concerned with is that some except was raised, or that no exception was.
|
137
|
-
|
138
|
-
h4. Throwing
|
139
|
-
|
140
|
-
<ruby>
|
141
|
-
proc.should_throw <symbol>
|
142
|
-
proc.should_not_throw <symbol>
|
143
|
-
</ruby>
|
144
|
-
|
145
|
-
Similar to the above, but checks that <code>symbol</code> is thrown from within <code>proc</code>, or not. The latter is actually one of two cases: some other symbol is thrown, or no symbol is thrown.
|
146
|
-
|
147
|
-
<ruby>
|
148
|
-
proc.should_not_throw
|
149
|
-
</ruby>
|
150
|
-
|
151
|
-
This form is more specific. It checks that no symbol is thrown from within <code>proc</code>.
|
152
|
-
|
153
|
-
h3. Collections
|
154
|
-
|
155
|
-
h4. Containment
|
156
|
-
|
157
|
-
<ruby>
|
158
|
-
target.should_include <object>
|
159
|
-
target.should_not_include <object>
|
160
|
-
</ruby>
|
161
|
-
|
162
|
-
This is simply a specific case of the arbitrary predicate form. It uses <code>target.include?(object)</code> and raises an <code>ExpectationNotMetError</code> if that returns false.
|
163
|
-
|
164
|
-
The remaining collection forms are a little more involved. They rely on two things: 1) <code>target</code> responds to the message <code>things</code> by returning an object that 2) responds to either <code>length</code> or <code>size</code>, which return a number that is a measure of size. Currently <code>length</code> is used if is appropriate, otherwise <code>size</code> is attempted.
|
165
|
-
|
166
|
-
h4. Exact Size
|
167
|
-
|
168
|
-
<ruby>
|
169
|
-
target.should_have(<number>).things
|
170
|
-
</ruby>
|
171
|
-
|
172
|
-
The <code>things</code> of <code>target</code> has a length/size of exactly <code>number</code>.
|
173
|
-
|
174
|
-
h4. Lower Bound
|
175
|
-
|
176
|
-
<ruby>
|
177
|
-
target.should_have_at_least(<number>).things
|
178
|
-
</ruby>
|
179
|
-
|
180
|
-
The <code>things</code> of <code>target</code> has a length/size of no less than <code>number</code>.
|
181
|
-
|
182
|
-
h4. Upper Bound
|
183
|
-
|
184
|
-
<ruby>
|
185
|
-
target.should_have_at_most(<number>).things
|
186
|
-
</ruby>
|
187
|
-
|
188
|
-
The <code>things</code> of <code>target</code> has a length/size of no more than <code>number</code>.
|
@@ -1,22 +0,0 @@
|
|
1
|
-
index.page:
|
2
|
-
inMenu: 11
|
3
|
-
|
4
|
-
underscores.page:
|
5
|
-
orderInfo: 13
|
6
|
-
|
7
|
-
mocks.page:
|
8
|
-
orderInfo: 14
|
9
|
-
|
10
|
-
rdoc.html:
|
11
|
-
title: RDoc
|
12
|
-
dest: ../rdoc/index.html
|
13
|
-
inMenu: true
|
14
|
-
orderInfo: 15
|
15
|
-
|
16
|
-
rcov.html:
|
17
|
-
title: RCov
|
18
|
-
dest: ../coverage/index.html
|
19
|
-
inMenu: true
|
20
|
-
orderInfo: 16
|
21
|
-
|
22
|
-
|
@@ -1,287 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Mock API
|
3
|
-
inMenu: true
|
4
|
-
---
|
5
|
-
h2. Mock API
|
6
|
-
|
7
|
-
RSpec contains a tightly integrated, powerful Mock Object framework.
|
8
|
-
|
9
|
-
h3. Creating a mock
|
10
|
-
|
11
|
-
<ruby>
|
12
|
-
my_mock = mock(<name>)
|
13
|
-
</ruby>
|
14
|
-
|
15
|
-
This creates a new mock with the given <code>name</code> (a string) and registers it.
|
16
|
-
When the specification finishes, all registered mocks are verified.
|
17
|
-
|
18
|
-
<ruby>
|
19
|
-
my_mock = mock(<name>, <options>)
|
20
|
-
</ruby>
|
21
|
-
|
22
|
-
As above, but allows you to specific options to tweak the mock's behaviour. The
|
23
|
-
<code>options</code> argument is a hash. Currently the only supported option is
|
24
|
-
<code>:null_object</code>. Setting this to true instructs the mock to ignore
|
25
|
-
(quietly consume) any messages it hasn't been told to expect - and return itself. I.e.:
|
26
|
-
|
27
|
-
<ruby>
|
28
|
-
my_mock = mock("blah", :null_object => true)
|
29
|
-
</ruby>
|
30
|
-
|
31
|
-
h3. Expecting Messages
|
32
|
-
|
33
|
-
<ruby>
|
34
|
-
my_mock.should_receive(<message>)
|
35
|
-
</ruby>
|
36
|
-
|
37
|
-
The <code>message</code> argument is a symbol that is the name of a message
|
38
|
-
that you want the mock to expect.
|
39
|
-
|
40
|
-
h3. Expecting Arguments
|
41
|
-
|
42
|
-
<ruby>
|
43
|
-
my_mock.should_receive(:msg).with(<args>)
|
44
|
-
</ruby>
|
45
|
-
|
46
|
-
for example:
|
47
|
-
|
48
|
-
<ruby>
|
49
|
-
my_mock.should_receive(:msg).with(1, 2, 3)
|
50
|
-
</ruby>
|
51
|
-
|
52
|
-
The <code>args</code> argument is a series of arguments (e.g. 1, 2, 3) that are
|
53
|
-
expected to be passed as arguments to the associated message.
|
54
|
-
|
55
|
-
<ruby>
|
56
|
-
my_mock.should_receive(:msg).with(:no_args)
|
57
|
-
</ruby>
|
58
|
-
|
59
|
-
The message (<code>msg</code>) is expected to be passed no arguments.
|
60
|
-
|
61
|
-
<ruby>
|
62
|
-
my_mock.should_receive(:msg).with(:any_args)
|
63
|
-
</ruby>
|
64
|
-
|
65
|
-
Any arguments (and any number of arguments) are to be accepted. This includes
|
66
|
-
cases where no arguments are provided. *This is the default when no <code>with()</code>
|
67
|
-
clause is specified.* Even so, sometimes you want to be explicit about it.
|
68
|
-
|
69
|
-
h3. Argument Constraints
|
70
|
-
|
71
|
-
Constraints can be placed on individual arguments which are looser than value
|
72
|
-
equivalence (as above).
|
73
|
-
|
74
|
-
h4. :anything
|
75
|
-
|
76
|
-
accepts any value for this argument, e.g.:
|
77
|
-
|
78
|
-
<ruby>
|
79
|
-
my_mock.should_receive(:msg).with(1, :anything, "A")
|
80
|
-
</ruby>
|
81
|
-
|
82
|
-
h4. :numeric
|
83
|
-
|
84
|
-
accepts any numeric value for this argument, e.g.:
|
85
|
-
|
86
|
-
<ruby>
|
87
|
-
my_mock.should_receive(:msg).with(a, :numeric, "b")
|
88
|
-
</ruby>
|
89
|
-
|
90
|
-
h4. :boolean
|
91
|
-
|
92
|
-
accepts a boolean value for this argument, e.g.:
|
93
|
-
|
94
|
-
<ruby>
|
95
|
-
my_mock.should_receive(:msg).with(a, :boolean, "b")
|
96
|
-
</ruby>
|
97
|
-
|
98
|
-
h4. :string
|
99
|
-
|
100
|
-
accepts any string for this argument, e.g.:
|
101
|
-
|
102
|
-
<ruby>
|
103
|
-
my_mock.should_receive(:msg).with(a, :string, "b")
|
104
|
-
</ruby>
|
105
|
-
|
106
|
-
h4. duck_type(message(s))
|
107
|
-
|
108
|
-
accepts any object that responds to the prescribed message(s), e.g.:
|
109
|
-
|
110
|
-
<ruby>
|
111
|
-
#accepts a Fixnum for the second arg
|
112
|
-
my_mock.should_receive(:msg).with(a, duck_type(:abs, :div), "b")
|
113
|
-
</ruby>
|
114
|
-
|
115
|
-
h3. Receive Counts
|
116
|
-
|
117
|
-
<ruby>
|
118
|
-
my_mock.should_receive(:msg).never
|
119
|
-
</ruby>
|
120
|
-
|
121
|
-
An exception is raised if the message is ever received.
|
122
|
-
This is equivalent to not specifying the reception of :msg,
|
123
|
-
but it's more declarative and useful for humans.
|
124
|
-
|
125
|
-
<ruby>
|
126
|
-
my_mock.should_receive(:msg).any_number_of_times
|
127
|
-
</ruby>
|
128
|
-
|
129
|
-
The message can be received 0 or more times.
|
130
|
-
|
131
|
-
<ruby>
|
132
|
-
my_mock.should_receive(:msg).once
|
133
|
-
</ruby>
|
134
|
-
|
135
|
-
An exception is raised if the message is never received, or it is received more than once.
|
136
|
-
|
137
|
-
<ruby>
|
138
|
-
my_mock.should_receive(:msg).twice
|
139
|
-
</ruby>
|
140
|
-
|
141
|
-
An exception is raised if the message is received anything but two times.
|
142
|
-
|
143
|
-
<ruby>
|
144
|
-
my_mock.should_receive(:msg).exactly(n).times
|
145
|
-
</ruby>
|
146
|
-
|
147
|
-
An exception is raised if the message is received anything but <code>n</code> times.
|
148
|
-
|
149
|
-
<ruby>
|
150
|
-
my_mock.should_receive(:msg).at_least(:once)
|
151
|
-
</ruby>
|
152
|
-
|
153
|
-
An exception is raised if the message is never received.
|
154
|
-
|
155
|
-
<ruby>
|
156
|
-
my_mock.should_receive(:msg).at_least(:twice)
|
157
|
-
</ruby>
|
158
|
-
|
159
|
-
An exception is raised if the message is never received or is received only once.
|
160
|
-
|
161
|
-
<ruby>
|
162
|
-
my_mock.should_receive(:msg).at_least(n).times
|
163
|
-
</ruby>
|
164
|
-
|
165
|
-
An exception is raised if the message is received fewer than <code>n</code> times.
|
166
|
-
|
167
|
-
h3. Return Values
|
168
|
-
|
169
|
-
h4. Single return value
|
170
|
-
|
171
|
-
<ruby>
|
172
|
-
my_mock.should_receive(:msg).once_and_return(<value>)
|
173
|
-
</ruby>
|
174
|
-
|
175
|
-
Each time the expected message is received, <code>value</code> will be returned as the result.
|
176
|
-
|
177
|
-
h4. Consequtive return values
|
178
|
-
|
179
|
-
<ruby>
|
180
|
-
and_return([<value-1>, <value-2>, ..., <value-n>])
|
181
|
-
</ruby>
|
182
|
-
|
183
|
-
When the expected message is received, <code>value-i</code> will be returned as the result
|
184
|
-
for the ith reception of the message. After the message has been received <code>i</code> times,
|
185
|
-
<code>value-n</code> is returned for all
|
186
|
-
subsequent receives.
|
187
|
-
|
188
|
-
*Note:* if you wish to have a single return value that is an array, you must use this form, with
|
189
|
-
one item that is the array to return. Otherwise your array will be interpreted as a series of return values.
|
190
|
-
For example:
|
191
|
-
|
192
|
-
<ruby>
|
193
|
-
and_return([[1, 2, 3]])
|
194
|
-
</ruby>
|
195
|
-
|
196
|
-
h4. Computed return value
|
197
|
-
|
198
|
-
<ruby>
|
199
|
-
my_mock.should_receive(:msg).once_and_return {...}
|
200
|
-
</ruby>
|
201
|
-
|
202
|
-
When the expected message is received, the result of evaluating the supplied
|
203
|
-
block will be returned as the result. The block is passed any arguments passed
|
204
|
-
as arguments of the message. This capability can be used to compute return
|
205
|
-
values based on the arguments. For example:
|
206
|
-
|
207
|
-
<ruby>
|
208
|
-
my_mock.should_receive(:msg).with(:numeric, :numeric) once_and_return {|a, b| a + b}
|
209
|
-
</ruby>
|
210
|
-
|
211
|
-
h3. Raising and Throwing
|
212
|
-
|
213
|
-
<ruby>
|
214
|
-
my_mock.should_receive(:msg).once_and_raise(<exception>)
|
215
|
-
my_mock.should_receive(:msg).once_and_throw(<symbol>)
|
216
|
-
</ruby>
|
217
|
-
|
218
|
-
These instruct the mock to raise an exception or throw a symbol, respectively,
|
219
|
-
instead of returning a value.
|
220
|
-
|
221
|
-
h3. Yielding
|
222
|
-
|
223
|
-
<ruby>
|
224
|
-
my_mock.should_receive(:msg).once_and_yield([<value-1>, <value-2>, ..., <value-n>])
|
225
|
-
</ruby>
|
226
|
-
|
227
|
-
When the expected message is received, the mock will yield the values to the passed block.
|
228
|
-
|
229
|
-
h3. Ordering
|
230
|
-
|
231
|
-
There are times when you want to specify the order of messages sent to a mock.
|
232
|
-
It shouldn't be the case very often, but it can be handy at times.
|
233
|
-
|
234
|
-
Labeling expectations as being ordered is done by the <code>ordered</code> call:
|
235
|
-
|
236
|
-
<ruby>
|
237
|
-
my_mock.should_receive(:flip).once.ordered
|
238
|
-
my_mock.should_receive(:flop).once.ordered
|
239
|
-
</ruby>
|
240
|
-
|
241
|
-
If the send of <code>flop</code> is seen before <code>flip</code> the specification will fail.
|
242
|
-
|
243
|
-
Of course, chains of ordered expectations can be set up:
|
244
|
-
|
245
|
-
<ruby>
|
246
|
-
my_mock.should_receive(:one).ordered
|
247
|
-
my_mock.should_receive(:two).ordered
|
248
|
-
my_mock.should_receive(:three).ordered
|
249
|
-
</ruby>
|
250
|
-
|
251
|
-
The expected order is the order in which the expectations are declared.
|
252
|
-
|
253
|
-
Order-independent expectations can be set anywhere in the expectation sequence, in any order.
|
254
|
-
Only the order of expectations tagged with the <code>ordered</code> call is significant.
|
255
|
-
Likewise, calls to order-independent methods can be made in any order, even interspersed with
|
256
|
-
calls to order-dependent methods. For example:
|
257
|
-
|
258
|
-
<ruby>
|
259
|
-
my_mock.should_receive(:zero)
|
260
|
-
my_mock.should_receive(:one).ordered
|
261
|
-
my_mock.should_receive(:two).ordered
|
262
|
-
my_mock.should_receive(:one_and_a_half)
|
263
|
-
|
264
|
-
# This will pass:
|
265
|
-
my_mock.one
|
266
|
-
my_mock.one_and_a_half
|
267
|
-
my_mock.zero
|
268
|
-
my_mock.two
|
269
|
-
</ruby>
|
270
|
-
|
271
|
-
h3. Arbitrary Handling of Received Messages
|
272
|
-
|
273
|
-
You can supply a block to a message expectation. When the message is received
|
274
|
-
by the mock, the block is passed any arguments and evaluated. The result is
|
275
|
-
the return value of the block. For example:
|
276
|
-
|
277
|
-
<ruby>
|
278
|
-
my_mock.should_receive(:msg) { |a, b|
|
279
|
-
a.should_be true
|
280
|
-
b.should_not_contain('mice')
|
281
|
-
"Chunky bacon!"
|
282
|
-
}
|
283
|
-
</ruby>
|
284
|
-
|
285
|
-
This allows arbitrary argument validation and result computation. It's handy and kind of cool to be able to
|
286
|
-
do this, but it is advised to not use this form in most situations. Mocks should not be functional.
|
287
|
-
They should be completely declarative. That said, it's sometimes useful to give them some minimal behaviour.
|