@@ -138,14 +138,15 @@ def test_init_mock_with_iterable_side_effect():
138
138
that returns the next item in the iterable each time the mock object is
139
139
called.
140
140
"""
141
-
141
+
142
142
mock = Mock (side_effect = [1 , 2 , 3 ])
143
143
assert mock () == 1 , "First call did not return 1."
144
144
assert mock () == 2 , "Second call did not return 2."
145
145
assert mock () == 3 , "Third call did not return 3."
146
146
with upytest .raises (StopIteration ):
147
147
mock ()
148
148
149
+
149
150
def test_init_mock_with_invalid_side_effect ():
150
151
"""
151
152
If an invalid side effect is specified, a TypeError should be raised.
@@ -154,10 +155,240 @@ def test_init_mock_with_invalid_side_effect():
154
155
with upytest .raises (TypeError ):
155
156
mock ()
156
157
158
+
157
159
def test_init_mock_with_return_value ():
158
160
"""
159
161
A Mock object should be created with the specified return value that is
160
162
returned each time the mock object is called.
161
163
"""
162
164
mock = Mock (return_value = 42 )
163
- assert mock () == 42 , "Return value not as expected."
165
+ assert mock () == 42 , "Return value not as expected."
166
+
167
+
168
+ def test_init_mock_with_kwargs ():
169
+ """
170
+ A Mock object should be created with the specified keyword arguments turned
171
+ into attributes on the mock object.
172
+ """
173
+ mock = Mock (foo = 1 , bar = 2 )
174
+ assert mock .foo == 1 , "Attribute 'foo' not set correctly."
175
+ assert mock .bar == 2 , "Attribute 'bar' not set correctly."
176
+
177
+
178
+ def test_reset_mock ():
179
+ """
180
+ Resetting the mock clears the call related data.
181
+ """
182
+ m = Mock ()
183
+ assert m .call_count == 0
184
+ m ()
185
+ assert m .call_count == 1
186
+ m .reset_mock ()
187
+ assert m .call_count == 0
188
+
189
+
190
+ def test_call_count ():
191
+ """
192
+ The call_count attribute should return the number of times the mock object
193
+ has been called.
194
+ """
195
+ m = Mock ()
196
+ assert m .call_count == 0
197
+ m ()
198
+ assert m .call_count == 1
199
+ m ()
200
+ assert m .call_count == 2
201
+
202
+
203
+ def test_called ():
204
+ """
205
+ The called attribute should return True if the mock object has been called
206
+ at least once.
207
+ """
208
+ m = Mock ()
209
+ assert not m .called
210
+ m ()
211
+ assert m .called
212
+
213
+
214
+ def test_call_args ():
215
+ """
216
+ The call_args attribute should return the arguments and keyword arguments
217
+ of the last call to the mock object.
218
+ """
219
+ m = Mock ()
220
+ assert m .call_args is None
221
+ m (1 , 2 , a = 3 , b = 4 )
222
+ assert m .call_args == ((1 , 2 ), {"a" : 3 , "b" : 4 })
223
+
224
+
225
+ def test_call_args_list ():
226
+ """
227
+ The call_args_list attribute should return a list of tuples containing the
228
+ arguments and keyword arguments of each call to the mock object.
229
+ """
230
+ m = Mock ()
231
+ assert m .call_args_list == []
232
+ m (1 , 2 , a = 3 , b = 4 )
233
+ m (5 , 6 , a = 7 , b = 8 )
234
+ assert m .call_args_list == [
235
+ ((1 , 2 ), {"a" : 3 , "b" : 4 }),
236
+ ((5 , 6 ), {"a" : 7 , "b" : 8 }),
237
+ ]
238
+
239
+
240
+ def test_assert_called ():
241
+ """
242
+ The assert_called method should raise an AssertionError if the mock object
243
+ has not been called.
244
+ """
245
+ m = Mock ()
246
+ with upytest .raises (AssertionError ):
247
+ m .assert_called ()
248
+ m ()
249
+ m .assert_called ()
250
+
251
+
252
+ def test_assert_called_once ():
253
+ """
254
+ The assert_called_once method should raise an AssertionError if the mock
255
+ object has not been called exactly once.
256
+ """
257
+ m = Mock ()
258
+ with upytest .raises (AssertionError ):
259
+ m .assert_called_once ()
260
+ m ()
261
+ m .assert_called_once ()
262
+ m ()
263
+ with upytest .raises (AssertionError ):
264
+ m .assert_called_once ()
265
+
266
+
267
+ def test_assert_called_with ():
268
+ """
269
+ The assert_called_with method should raise an AssertionError if the mock
270
+ object not last called with the specified arguments.
271
+ """
272
+ m = Mock ()
273
+ m (1 , 2 , a = 3 , b = 4 )
274
+ m .assert_called_with (1 , 2 , a = 3 , b = 4 )
275
+ with upytest .raises (AssertionError ):
276
+ m .assert_called_with (1 , 3 , a = 3 , b = 4 )
277
+ with upytest .raises (AssertionError ):
278
+ m .assert_called_with (1 , 2 , a = 3 , b = 5 )
279
+
280
+
281
+ def test_assert_called_once_with ():
282
+ """
283
+ The assert_called_once_with method should raise an AssertionError if the
284
+ mock object has not been called exactly once with the specified arguments.
285
+ """
286
+ m = Mock ()
287
+ m (1 , 2 , a = 3 , b = 4 )
288
+ m .assert_called_once_with (1 , 2 , a = 3 , b = 4 )
289
+ with upytest .raises (AssertionError ):
290
+ m .assert_called_once_with (1 , 3 , a = 3 , b = 4 )
291
+ with upytest .raises (AssertionError ):
292
+ m .assert_called_once_with (1 , 2 , a = 3 , b = 5 )
293
+ m (1 , 2 , a = 3 , b = 4 )
294
+ with upytest .raises (AssertionError ):
295
+ m .assert_called_once_with (1 , 2 , a = 3 , b = 4 )
296
+
297
+
298
+ def test_assert_any_call ():
299
+ """
300
+ The assert_any_call method should raise an AssertionError if the mock
301
+ object has not been called with the specified arguments at least once.
302
+ """
303
+ m = Mock ()
304
+ with upytest .raises (AssertionError ):
305
+ m .assert_any_call (1 , 2 , a = 3 , b = 4 )
306
+ m (1 , 2 , a = 3 , b = 4 )
307
+ m (5 , 6 , a = 7 , b = 8 )
308
+ m .assert_any_call (1 , 2 , a = 3 , b = 4 )
309
+
310
+
311
+ def test_assert_has_calls ():
312
+ """
313
+ The assert_has_calls method should raise an AssertionError if the mock
314
+ object has not been called with the specified arguments at least once.
315
+ """
316
+ m = Mock ()
317
+ with upytest .raises (AssertionError ):
318
+ # No calls have been made yet.
319
+ m .assert_has_calls ([((1 , 2 ), {"a" : 3 , "b" : 4 })])
320
+ m (1 , 2 , a = 3 , b = 4 )
321
+ m (5 , 6 , a = 7 , b = 8 )
322
+ # Calls have been made.
323
+ m .assert_has_calls (
324
+ [((1 , 2 ), {"a" : 3 , "b" : 4 }), ((5 , 6 ), {"a" : 7 , "b" : 8 })]
325
+ )
326
+ with upytest .raises (AssertionError ):
327
+ # Calls have been made, but not in the specified order.
328
+ m .assert_has_calls (
329
+ [((5 , 6 ), {"a" : 7 , "b" : 9 }), ((1 , 2 ), {"a" : 3 , "b" : 4 })]
330
+ )
331
+ with upytest .raises (AssertionError ):
332
+ # Calls have been made, but not with the specified arguments.
333
+ m .assert_has_calls (
334
+ [((1 , 2 ), {"a" : 3 , "b" : 4 }), ((5 , 6 ), {"a" : 7 , "b" : 9 })]
335
+ )
336
+
337
+
338
+ def test_assert_has_call_any_order ():
339
+ """
340
+ The assert_has_calls method should raise an AssertionError if the mock
341
+ object has not been called with the specified arguments at least once.
342
+ """
343
+ m = Mock ()
344
+ with upytest .raises (AssertionError ):
345
+ # No calls have been made yet.
346
+ m .assert_has_calls ([((1 , 2 ), {"a" : 3 , "b" : 4 })], any_order = True )
347
+ m (1 , 2 , a = 3 , b = 4 )
348
+ m (5 , 6 , a = 7 , b = 8 )
349
+ # Calls have been made, no matter the order.
350
+ m .assert_has_calls (
351
+ [((5 , 6 ), {"a" : 7 , "b" : 8 }), ((1 , 2 ), {"a" : 3 , "b" : 4 })],
352
+ any_order = True ,
353
+ )
354
+ with upytest .raises (AssertionError ):
355
+ # Calls have been made, but not with the specified arguments.
356
+ m .assert_has_calls (
357
+ [((1 , 2 ), {"a" : 3 , "b" : 4 }), ((5 , 6 ), {"a" : 7 , "b" : 9 })],
358
+ any_order = True ,
359
+ )
360
+
361
+
362
+ def test_assert_never_called ():
363
+ """
364
+ The assert_never_called method should raise an AssertionError if the mock
365
+ object has been called at least once.
366
+ """
367
+ m = Mock ()
368
+ # OK, no calls.
369
+ m .assert_never_called ()
370
+ m ()
371
+ with upytest .raises (AssertionError ):
372
+ # Not OK, a call has been made.
373
+ m .assert_never_called ()
374
+
375
+
376
+ def assert_call_returns_same_unique_mock ():
377
+ """
378
+ The mock object should return the same unique mock object each time it is
379
+ called.
380
+ """
381
+ m = Mock ()
382
+ mock_result = m ()
383
+ assert id (m ) != id (mock_result ), "Returned mock object is not unique."
384
+ assert id (m ()) == id (mock_result ), "Returned mock object is not the same."
385
+
386
+
387
+ def test_get_attribute_of_unknown_attribute_returns_mock ():
388
+ """
389
+ Accessing an unknown attribute on the mock object should return another
390
+ mock object. Each attribute access should return the same unique mock.
391
+ """
392
+ m = Mock ()
393
+ assert isinstance (m .foo , Mock ), "Returned object is not a Mock."
394
+ assert id (m .foo ) == id (m .foo ), "Returned object is not the same."
0 commit comments