-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubles_spec.rb
283 lines (225 loc) · 6.94 KB
/
doubles_spec.rb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
describe 'Doubles' do
it "allow stubbing methods" do
dbl = double("Chant")
allow(dbl).to receive(:hey!)
expect(dbl).to respond_to(:hey!)
end
it "allow stubbing a response with a block" do
dbl = double("Chant")
# When I say 'Hey!' you say 'Ho!'
allow(dbl).to receive(:hey!) { "Ho!" }
# "Hey!", "Ho!"
expect(dbl.hey!).to eq("Ho!")
end
it "allow stubbing responses with #and_return" do
dbl = double("Chant")
# When I say 'Hey!' you say 'Ho!'
allow(dbl).to receive(:hey!).and_return("Ho!")
# "Hey!", "Ho!"
expect(dbl.hey!).to eq("Ho!")
end
it "allow stubbing multiple methods with hash syntax" do
dbl = double("Person")
# Note this uses #receive_messages, not #receive
allow(dbl).to receive_messages(:full_name => 'Mary Smith',
:initials => 'MTS')
expect(dbl.full_name).to eq("Mary Smith")
expect(dbl.initials).to eq("MTS")
end
it "allow stubbing with a hash argument to #double" do
dbl = double("Person", :full_name => 'Mary Smith',
:initials => 'MTS')
expect(dbl.full_name).to eq("Mary Smith")
expect(dbl.initials).to eq("MTS")
end
it "allow stubbing multiple responses with #and_return" do
die = double("Die")
allow(die).to receive(:roll).and_return(1,5,2,6)
expect(die.roll).to eq(1)
expect(die.roll).to eq(5)
expect(die.roll).to eq(2)
expect(die.roll).to eq(6)
expect(die.roll).to eq(6) # continues returning last value
end
context 'with partial test doubles' do
it "allows stubbing instance methods on Ruby classes" do
time = Time.new(2010, 1, 1, 12, 0, 0)
allow(time).to receive(:year).and_return(1975)
expect(time.to_s).to eq('2010-01-01 12:00:00 -0600')
expect(time.year).to eq(1975)
end
it "allows stubbing instance methods on custom classes" do
class SuperHero
attr_accessor :name
end
hero = SuperHero.new
hero.name = 'Superman'
expect(hero.name).to eq('Superman')
allow(hero).to receive(:name).and_return('Clark Kent')
expect(hero.name).to eq('Clark Kent')
end
it "allows stubbing class methods on Ruby classes" do
fixed = Time.new(2010, 1, 1, 12, 0, 0)
allow(Time).to receive(:now).and_return(fixed)
expect(Time.now).to eq(fixed)
expect(Time.now.to_s).to eq('2010-01-01 12:00:00 -0600')
expect(Time.now.year).to eq(2010)
end
it "allows stubbing database calls on a mock object" do
class Customer
attr_accessor :name
def self.find
# database lookup, returns one object
end
end
dbl = double('Mock Customer')
allow(dbl).to receive(:name).and_return('Bob')
allow(Customer).to receive(:find).and_return(dbl)
customer = Customer.find
expect(customer.name).to eq('Bob')
end
it "allows stubbing database calls with many mock objects" do
class Customer
attr_accessor :name
def self.all
# database lookup, returns array of objects
end
end
c1 = double('First Customer', :name => 'Bob')
c2 = double('Second Customer', :name => 'Mary')
allow(Customer).to receive(:all).and_return([c1, c2])
customers = Customer.all
expect(customers[1].name).to eq('Mary')
end
end
context 'with message expectations' do
it "expects a call and allows a response" do
dbl = double("Chant")
expect(dbl).to receive(:hey!).and_return("Ho!")
dbl.hey!
end
it "does not matter which order" do
dbl = double("Multi-step Process")
expect(dbl).to receive(:step_1)
expect(dbl).to receive(:step_2)
dbl.step_2
dbl.step_1
end
it "works with #ordered when order matters" do
dbl = double("Multi-step Process")
expect(dbl).to receive(:step_1).ordered
expect(dbl).to receive(:step_2).ordered
dbl.step_1
dbl.step_2
end
end
context 'with message argument constraints' do
it "expects arguments will match" do
dbl = double("Customer List")
expect(dbl).to receive(:sort).with('name')
dbl.sort('name')
end
it "passes when any arguments are allowed" do
dbl = double("Customer List")
# The default if you don't use #with
expect(dbl).to receive(:sort).with(any_args)
dbl.sort('name')
end
it "works the same with multiple arguments" do
dbl = double("Customer List")
expect(dbl).to receive(:sort).with('name', 'asc', true)
dbl.sort('name', 'asc', true)
end
it "allows constraining only some arguments" do
dbl = double("Customer List")
expect(dbl).to receive(:sort).with('name', anything, anything)
dbl.sort('name', 'asc', true)
end
it "allows using other matchers" do
dbl = double("Customer List")
expect(dbl).to receive(:sort).with(
a_string_starting_with('n'),
an_object_eq_to('asc') | an_object_eq_to('desc'),
boolean
)
dbl.sort('name', 'asc', true)
end
end
context 'with message count constraints' do
it "allows constraints on message count" do
class Cart
def initialize; @items = []; end
def add_item(id); @items << id; end
def restock_item(id); @items.delete(id); end
def empty; @items.each { |id| restock_item(id) }; end
end
cart = Cart.new
cart.add_item(35)
cart.add_item(178)
expect(cart).to receive(:restock_item).twice
cart.empty
end
it "allows using at_least/at_most" do
post = double('BlogPost')
expect(post).to receive(:like).at_least(3).times
post.like(:user => 'Bob')
post.like(:user => 'Mary')
post.like(:user => 'Ted')
post.like(:user => 'Jane')
end
end
context 'with spying abilities' do
it "can expect a call after it is received" do
dbl = spy("Chant")
allow(dbl).to receive(:hey!).and_return("Ho!")
dbl.hey!
expect(dbl).to have_received(:hey!)
end
it "can use message constraints" do
dbl = spy("Chant")
allow(dbl).to receive(:hey!).and_return("Ho!")
dbl.hey!
dbl.hey!
dbl.hey!
expect(dbl).to have_received(:hey!).with(no_args).exactly(3).times
end
it "can expect any message alraedy sent to a declared spy" do
customer = spy("Customer")
# Notice that we don't stub :send_invoice
# allow(customer).to receive(:send_invoice)
customer.send_invoice
expect(customer).to have_received(:send_invoice)
end
it "can expect only allowed messages on partial doubls" do
class Customer
def send_invoice; true; end
end
customer = Customer.new
# Must stub :send_invoice to start spying
allow(customer).to receive(:send_invoice)
customer.send_invoice
expect(customer).to have_received(:send_invoice)
end
context 'using let and a before hook' do
let(:order) do
spy('Order', :process_line_items => nil,
:charge_credit_card => true,
:send_confirmation_email => true)
end
before(:example) do
order.process_line_items
order.charge_credit_card
order.send_confirmation_email
end
it 'calls #process_line_items on the order' do
expect(order).to have_received(:process_line_items)
end
it 'calls #charge_credit_card on the order' do
expect(order).to have_received(:charge_credit_card)
end
it 'calls #send_confirmation_email on the order' do
expect(order).to have_received(:send_confirmation_email)
end
end
end
end