blob: 1b3dd2f896a897aa389a667293ffa2b65a49dd92 [file] [log] [blame]
Michael Foord1e68bec2012-03-03 22:24:30 +00001# Copyright (C) 2007-2012 Michael Foord & the mock team
2# E-mail: fuzzyman AT voidspace DOT org DOT uk
3# http://www.voidspace.org.uk/python/mock/
4
5from tests.support import unittest2, inPy3k
6
7from mock import (
8 call, _Call, create_autospec,
Michael Foorda9a8bb92012-03-13 14:39:59 -07009 MagicMock, Mock, ANY, _CallList
Michael Foord1e68bec2012-03-03 22:24:30 +000010)
11
12from datetime import datetime
13
14class SomeClass(object):
15 def one(self, a, b):
16 pass
17 def two(self):
18 pass
19 def three(self, a=None):
20 pass
21
22
23
24class AnyTest(unittest2.TestCase):
25
26 def test_any(self):
27 self.assertEqual(ANY, object())
28
29 mock = Mock()
30 mock(ANY)
31 mock.assert_called_with(ANY)
32
33 mock = Mock()
34 mock(foo=ANY)
35 mock.assert_called_with(foo=ANY)
36
37 def test_repr(self):
38 self.assertEqual(repr(ANY), '<ANY>')
39 self.assertEqual(str(ANY), '<ANY>')
40
41
42 def test_any_and_datetime(self):
43 mock = Mock()
44 mock(datetime.now(), foo=datetime.now())
45
46 mock.assert_called_with(ANY, foo=ANY)
47
48
49 def test_any_mock_calls_comparison_order(self):
50 mock = Mock()
51 d = datetime.now()
52 class Foo(object):
53 def __eq__(self, other):
54 return False
55 def __ne__(self, other):
56 return True
57
58 for d in datetime.now(), Foo():
59 mock.reset_mock()
60
61 mock(d, foo=d, bar=d)
62 mock.method(d, zinga=d, alpha=d)
63 mock().method(a1=d, z99=d)
64
65 expected = [
66 call(ANY, foo=ANY, bar=ANY),
67 call.method(ANY, zinga=ANY, alpha=ANY),
68 call(), call().method(a1=ANY, z99=ANY)
69 ]
70 self.assertEqual(expected, mock.mock_calls)
71 self.assertEqual(mock.mock_calls, expected)
72
73
74
75class CallTest(unittest2.TestCase):
76
77 def test_call_with_call(self):
78 kall = _Call()
79 self.assertEqual(kall, _Call())
80 self.assertEqual(kall, _Call(('',)))
81 self.assertEqual(kall, _Call(((),)))
82 self.assertEqual(kall, _Call(({},)))
83 self.assertEqual(kall, _Call(('', ())))
84 self.assertEqual(kall, _Call(('', {})))
85 self.assertEqual(kall, _Call(('', (), {})))
86 self.assertEqual(kall, _Call(('foo',)))
87 self.assertEqual(kall, _Call(('bar', ())))
88 self.assertEqual(kall, _Call(('baz', {})))
89 self.assertEqual(kall, _Call(('spam', (), {})))
90
91 kall = _Call(((1, 2, 3),))
92 self.assertEqual(kall, _Call(((1, 2, 3),)))
93 self.assertEqual(kall, _Call(('', (1, 2, 3))))
94 self.assertEqual(kall, _Call(((1, 2, 3), {})))
95 self.assertEqual(kall, _Call(('', (1, 2, 3), {})))
96
97 kall = _Call(((1, 2, 4),))
98 self.assertNotEqual(kall, _Call(('', (1, 2, 3))))
99 self.assertNotEqual(kall, _Call(('', (1, 2, 3), {})))
100
101 kall = _Call(('foo', (1, 2, 4),))
102 self.assertNotEqual(kall, _Call(('', (1, 2, 4))))
103 self.assertNotEqual(kall, _Call(('', (1, 2, 4), {})))
104 self.assertNotEqual(kall, _Call(('bar', (1, 2, 4))))
105 self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {})))
106
107 kall = _Call(({'a': 3},))
108 self.assertEqual(kall, _Call(('', (), {'a': 3})))
109 self.assertEqual(kall, _Call(('', {'a': 3})))
110 self.assertEqual(kall, _Call(((), {'a': 3})))
111 self.assertEqual(kall, _Call(({'a': 3},)))
112
113
114 def test_empty__Call(self):
115 args = _Call()
116
117 self.assertEqual(args, ())
118 self.assertEqual(args, ('foo',))
119 self.assertEqual(args, ((),))
120 self.assertEqual(args, ('foo', ()))
121 self.assertEqual(args, ('foo',(), {}))
122 self.assertEqual(args, ('foo', {}))
123 self.assertEqual(args, ({},))
124
125
126 def test_named_empty_call(self):
127 args = _Call(('foo', (), {}))
128
129 self.assertEqual(args, ('foo',))
130 self.assertEqual(args, ('foo', ()))
131 self.assertEqual(args, ('foo',(), {}))
132 self.assertEqual(args, ('foo', {}))
133
134 self.assertNotEqual(args, ((),))
135 self.assertNotEqual(args, ())
136 self.assertNotEqual(args, ({},))
137 self.assertNotEqual(args, ('bar',))
138 self.assertNotEqual(args, ('bar', ()))
139 self.assertNotEqual(args, ('bar', {}))
140
141
142 def test_call_with_args(self):
143 args = _Call(((1, 2, 3), {}))
144
145 self.assertEqual(args, ((1, 2, 3),))
146 self.assertEqual(args, ('foo', (1, 2, 3)))
147 self.assertEqual(args, ('foo', (1, 2, 3), {}))
148 self.assertEqual(args, ((1, 2, 3), {}))
149
150
151 def test_named_call_with_args(self):
152 args = _Call(('foo', (1, 2, 3), {}))
153
154 self.assertEqual(args, ('foo', (1, 2, 3)))
155 self.assertEqual(args, ('foo', (1, 2, 3), {}))
156
157 self.assertNotEqual(args, ((1, 2, 3),))
158 self.assertNotEqual(args, ((1, 2, 3), {}))
159
160
161 def test_call_with_kwargs(self):
162 args = _Call(((), dict(a=3, b=4)))
163
164 self.assertEqual(args, (dict(a=3, b=4),))
165 self.assertEqual(args, ('foo', dict(a=3, b=4)))
166 self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
167 self.assertEqual(args, ((), dict(a=3, b=4)))
168
169
170 def test_named_call_with_kwargs(self):
171 args = _Call(('foo', (), dict(a=3, b=4)))
172
173 self.assertEqual(args, ('foo', dict(a=3, b=4)))
174 self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
175
176 self.assertNotEqual(args, (dict(a=3, b=4),))
177 self.assertNotEqual(args, ((), dict(a=3, b=4)))
178
179
180 def test_call_with_args_call_empty_name(self):
181 args = _Call(((1, 2, 3), {}))
182 self.assertEqual(args, call(1, 2, 3))
183 self.assertEqual(call(1, 2, 3), args)
184 self.assertTrue(call(1, 2, 3) in [args])
185
186
187 def test_call_ne(self):
188 self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2))
189 self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3))
190 self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3))
191
192
193 def test_call_non_tuples(self):
194 kall = _Call(((1, 2, 3),))
195 for value in 1, None, self, int:
196 self.assertNotEqual(kall, value)
197 self.assertFalse(kall == value)
198
199
200 def test_repr(self):
201 self.assertEqual(repr(_Call()), 'call()')
202 self.assertEqual(repr(_Call(('foo',))), 'call.foo()')
203
204 self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))),
205 "call(1, 2, 3, a='b')")
206 self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))),
207 "call.bar(1, 2, 3, a='b')")
208
209 self.assertEqual(repr(call), 'call')
210 self.assertEqual(str(call), 'call')
211
212 self.assertEqual(repr(call()), 'call()')
213 self.assertEqual(repr(call(1)), 'call(1)')
214 self.assertEqual(repr(call(zz='thing')), "call(zz='thing')")
215
216 self.assertEqual(repr(call().foo), 'call().foo')
217 self.assertEqual(repr(call(1).foo.bar(a=3).bing),
218 'call().foo.bar().bing')
219 self.assertEqual(
220 repr(call().foo(1, 2, a=3)),
221 "call().foo(1, 2, a=3)"
222 )
223 self.assertEqual(repr(call()()), "call()()")
224 self.assertEqual(repr(call(1)(2)), "call()(2)")
225 self.assertEqual(
226 repr(call()().bar().baz.beep(1)),
227 "call()().bar().baz.beep(1)"
228 )
229
230
231 def test_call(self):
232 self.assertEqual(call(), ('', (), {}))
233 self.assertEqual(call('foo', 'bar', one=3, two=4),
234 ('', ('foo', 'bar'), {'one': 3, 'two': 4}))
235
236 mock = Mock()
237 mock(1, 2, 3)
238 mock(a=3, b=6)
239 self.assertEqual(mock.call_args_list,
240 [call(1, 2, 3), call(a=3, b=6)])
241
242 def test_attribute_call(self):
243 self.assertEqual(call.foo(1), ('foo', (1,), {}))
244 self.assertEqual(call.bar.baz(fish='eggs'),
245 ('bar.baz', (), {'fish': 'eggs'}))
246
247 mock = Mock()
248 mock.foo(1, 2 ,3)
249 mock.bar.baz(a=3, b=6)
250 self.assertEqual(mock.method_calls,
251 [call.foo(1, 2, 3), call.bar.baz(a=3, b=6)])
252
253
254 def test_extended_call(self):
255 result = call(1).foo(2).bar(3, a=4)
256 self.assertEqual(result, ('().foo().bar', (3,), dict(a=4)))
257
258 mock = MagicMock()
259 mock(1, 2, a=3, b=4)
260 self.assertEqual(mock.call_args, call(1, 2, a=3, b=4))
261 self.assertNotEqual(mock.call_args, call(1, 2, 3))
262
263 self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)])
264 self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)])
265
266 mock = MagicMock()
267 mock.foo(1).bar()().baz.beep(a=6)
268
269 last_call = call.foo(1).bar()().baz.beep(a=6)
270 self.assertEqual(mock.mock_calls[-1], last_call)
271 self.assertEqual(mock.mock_calls, last_call.call_list())
272
273
274 def test_call_list(self):
275 mock = MagicMock()
276 mock(1)
277 self.assertEqual(call(1).call_list(), mock.mock_calls)
278
279 mock = MagicMock()
280 mock(1).method(2)
281 self.assertEqual(call(1).method(2).call_list(),
282 mock.mock_calls)
283
284 mock = MagicMock()
285 mock(1).method(2)(3)
286 self.assertEqual(call(1).method(2)(3).call_list(),
287 mock.mock_calls)
288
289 mock = MagicMock()
290 int(mock(1).method(2)(3).foo.bar.baz(4)(5))
291 kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__()
292 self.assertEqual(kall.call_list(), mock.mock_calls)
293
294
295 def test_call_any(self):
296 self.assertEqual(call, ANY)
297
298 m = MagicMock()
299 int(m)
300 self.assertEqual(m.mock_calls, [ANY])
301 self.assertEqual([ANY], m.mock_calls)
302
303
304 def test_two_args_call(self):
305 args = _Call(((1, 2), {'a': 3}), two=True)
306 self.assertEqual(len(args), 2)
307 self.assertEqual(args[0], (1, 2))
308 self.assertEqual(args[1], {'a': 3})
309
310 other_args = _Call(((1, 2), {'a': 3}))
311 self.assertEqual(args, other_args)
312
313
314class SpecSignatureTest(unittest2.TestCase):
315
316 def _check_someclass_mock(self, mock):
317 self.assertRaises(AttributeError, getattr, mock, 'foo')
318 mock.one(1, 2)
319 mock.one.assert_called_with(1, 2)
320 self.assertRaises(AssertionError,
321 mock.one.assert_called_with, 3, 4)
322 self.assertRaises(TypeError, mock.one, 1)
323
324 mock.two()
325 mock.two.assert_called_with()
326 self.assertRaises(AssertionError,
327 mock.two.assert_called_with, 3)
328 self.assertRaises(TypeError, mock.two, 1)
329
330 mock.three()
331 mock.three.assert_called_with()
332 self.assertRaises(AssertionError,
333 mock.three.assert_called_with, 3)
334 self.assertRaises(TypeError, mock.three, 3, 2)
335
336 mock.three(1)
337 mock.three.assert_called_with(1)
338
339 mock.three(a=1)
340 mock.three.assert_called_with(a=1)
341
342
343 def test_basic(self):
344 for spec in (SomeClass, SomeClass()):
345 mock = create_autospec(spec)
346 self._check_someclass_mock(mock)
347
348
349 def test_create_autospec_return_value(self):
350 def f():
351 pass
352 mock = create_autospec(f, return_value='foo')
353 self.assertEqual(mock(), 'foo')
354
355 class Foo(object):
356 pass
357
358 mock = create_autospec(Foo, return_value='foo')
359 self.assertEqual(mock(), 'foo')
360
361
Michael Foord2b732bc2012-03-13 13:17:34 -0700362 @unittest2.expectedFailure
Michael Foord1e68bec2012-03-03 22:24:30 +0000363 def test_create_autospec_unbound_methods(self):
364 # see issue 128
365 class Foo(object):
366 def foo(self):
367 pass
368
369 klass = create_autospec(Foo)
370 instance = klass()
Michael Foorda87f30c2012-03-04 17:53:24 +0000371 self.assertRaises(TypeError, instance.foo, 1)
372
373 # Note: no type checking on the "self" parameter
374 klass.foo(1)
375 klass.foo.assert_called_with(1)
376 self.assertRaises(TypeError, klass.foo)
Michael Foord1e68bec2012-03-03 22:24:30 +0000377
378
379 def test_create_autospec_keyword_arguments(self):
380 class Foo(object):
381 a = 3
382 m = create_autospec(Foo, a='3')
383 self.assertEqual(m.a, '3')
384
385
386 def test_function_as_instance_attribute(self):
387 obj = SomeClass()
388 def f(a):
389 pass
390 obj.f = f
391
392 mock = create_autospec(obj)
393 mock.f('bing')
394 mock.f.assert_called_with('bing')
395
396
397 def test_spec_as_list(self):
398 # because spec as a list of strings in the mock constructor means
399 # something very different we treat a list instance as the type.
400 mock = create_autospec([])
401 mock.append('foo')
402 mock.append.assert_called_with('foo')
403
404 self.assertRaises(AttributeError, getattr, mock, 'foo')
405
406 class Foo(object):
407 foo = []
408
409 mock = create_autospec(Foo)
410 mock.foo.append(3)
411 mock.foo.append.assert_called_with(3)
412 self.assertRaises(AttributeError, getattr, mock.foo, 'foo')
413
414
415 def test_attributes(self):
416 class Sub(SomeClass):
417 attr = SomeClass()
418
419 sub_mock = create_autospec(Sub)
420
421 for mock in (sub_mock, sub_mock.attr):
422 self._check_someclass_mock(mock)
423
424
425 def test_builtin_functions_types(self):
Michael Foorda9a8bb92012-03-13 14:39:59 -0700426 # we could replace builtin functions / methods with a function
Michael Foord1e68bec2012-03-03 22:24:30 +0000427 # with *args / **kwargs signature. Using the builtin method type
428 # as a spec seems to work fairly well though.
429 class BuiltinSubclass(list):
430 def bar(self, arg):
431 pass
432 sorted = sorted
433 attr = {}
434
435 mock = create_autospec(BuiltinSubclass)
436 mock.append(3)
437 mock.append.assert_called_with(3)
438 self.assertRaises(AttributeError, getattr, mock.append, 'foo')
439
440 mock.bar('foo')
441 mock.bar.assert_called_with('foo')
442 self.assertRaises(TypeError, mock.bar, 'foo', 'bar')
443 self.assertRaises(AttributeError, getattr, mock.bar, 'foo')
444
445 mock.sorted([1, 2])
446 mock.sorted.assert_called_with([1, 2])
447 self.assertRaises(AttributeError, getattr, mock.sorted, 'foo')
448
449 mock.attr.pop(3)
450 mock.attr.pop.assert_called_with(3)
451 self.assertRaises(AttributeError, getattr, mock.attr, 'foo')
452
453
454 def test_method_calls(self):
455 class Sub(SomeClass):
456 attr = SomeClass()
457
458 mock = create_autospec(Sub)
459 mock.one(1, 2)
460 mock.two()
461 mock.three(3)
462
463 expected = [call.one(1, 2), call.two(), call.three(3)]
464 self.assertEqual(mock.method_calls, expected)
465
466 mock.attr.one(1, 2)
467 mock.attr.two()
468 mock.attr.three(3)
469
470 expected.extend(
471 [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)]
472 )
473 self.assertEqual(mock.method_calls, expected)
474
475
476 def test_magic_methods(self):
477 class BuiltinSubclass(list):
478 attr = {}
479
480 mock = create_autospec(BuiltinSubclass)
481 self.assertEqual(list(mock), [])
482 self.assertRaises(TypeError, int, mock)
483 self.assertRaises(TypeError, int, mock.attr)
484 self.assertEqual(list(mock), [])
485
486 self.assertIsInstance(mock['foo'], MagicMock)
487 self.assertIsInstance(mock.attr['foo'], MagicMock)
488
489
490 def test_spec_set(self):
491 class Sub(SomeClass):
492 attr = SomeClass()
493
494 for spec in (Sub, Sub()):
495 mock = create_autospec(spec, spec_set=True)
496 self._check_someclass_mock(mock)
497
498 self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar')
499 self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar')
500
501
502 def test_descriptors(self):
503 class Foo(object):
504 @classmethod
505 def f(cls, a, b):
506 pass
507 @staticmethod
508 def g(a, b):
509 pass
510
511 class Bar(Foo):
512 pass
513
514 class Baz(SomeClass, Bar):
515 pass
516
517 for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()):
518 mock = create_autospec(spec)
519 mock.f(1, 2)
520 mock.f.assert_called_once_with(1, 2)
521
522 mock.g(3, 4)
523 mock.g.assert_called_once_with(3, 4)
524
525
526 @unittest2.skipIf(inPy3k, "No old style classes in Python 3")
527 def test_old_style_classes(self):
528 class Foo:
529 def f(self, a, b):
530 pass
531
532 class Bar(Foo):
533 g = Foo()
534
535 for spec in (Foo, Foo(), Bar, Bar()):
536 mock = create_autospec(spec)
537 mock.f(1, 2)
538 mock.f.assert_called_once_with(1, 2)
539
540 self.assertRaises(AttributeError, getattr, mock, 'foo')
541 self.assertRaises(AttributeError, getattr, mock.f, 'foo')
542
543 mock.g.f(1, 2)
544 mock.g.f.assert_called_once_with(1, 2)
545 self.assertRaises(AttributeError, getattr, mock.g, 'foo')
546
547
548 def test_recursive(self):
549 class A(object):
550 def a(self):
551 pass
552 foo = 'foo bar baz'
553 bar = foo
554
555 A.B = A
556 mock = create_autospec(A)
557
558 mock()
559 self.assertFalse(mock.B.called)
560
561 mock.a()
562 mock.B.a()
563 self.assertEqual(mock.method_calls, [call.a(), call.B.a()])
564
565 self.assertIs(A.foo, A.bar)
566 self.assertIsNot(mock.foo, mock.bar)
567 mock.foo.lower()
568 self.assertRaises(AssertionError, mock.bar.lower.assert_called_with)
569
570
571 def test_spec_inheritance_for_classes(self):
572 class Foo(object):
573 def a(self):
574 pass
575 class Bar(object):
576 def f(self):
577 pass
578
579 class_mock = create_autospec(Foo)
580
581 self.assertIsNot(class_mock, class_mock())
582
583 for this_mock in class_mock, class_mock():
584 this_mock.a()
585 this_mock.a.assert_called_with()
586 self.assertRaises(TypeError, this_mock.a, 'foo')
587 self.assertRaises(AttributeError, getattr, this_mock, 'b')
588
589 instance_mock = create_autospec(Foo())
590 instance_mock.a()
591 instance_mock.a.assert_called_with()
592 self.assertRaises(TypeError, instance_mock.a, 'foo')
593 self.assertRaises(AttributeError, getattr, instance_mock, 'b')
594
595 # The return value isn't isn't callable
596 self.assertRaises(TypeError, instance_mock)
597
598 instance_mock.Bar.f()
599 instance_mock.Bar.f.assert_called_with()
600 self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g')
601
602 instance_mock.Bar().f()
603 instance_mock.Bar().f.assert_called_with()
604 self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g')
605
606
607 def test_inherit(self):
608 class Foo(object):
609 a = 3
610
611 Foo.Foo = Foo
612
613 # class
614 mock = create_autospec(Foo)
615 instance = mock()
616 self.assertRaises(AttributeError, getattr, instance, 'b')
617
618 attr_instance = mock.Foo()
619 self.assertRaises(AttributeError, getattr, attr_instance, 'b')
620
621 # instance
622 mock = create_autospec(Foo())
623 self.assertRaises(AttributeError, getattr, mock, 'b')
624 self.assertRaises(TypeError, mock)
625
626 # attribute instance
627 call_result = mock.Foo()
628 self.assertRaises(AttributeError, getattr, call_result, 'b')
629
630
631 def test_builtins(self):
632 # used to fail with infinite recursion
633 create_autospec(1)
634
635 create_autospec(int)
636 create_autospec('foo')
637 create_autospec(str)
638 create_autospec({})
639 create_autospec(dict)
640 create_autospec([])
641 create_autospec(list)
642 create_autospec(set())
643 create_autospec(set)
644 create_autospec(1.0)
645 create_autospec(float)
646 create_autospec(1j)
647 create_autospec(complex)
648 create_autospec(False)
649 create_autospec(True)
650
651
652 def test_function(self):
653 def f(a, b):
654 pass
655
656 mock = create_autospec(f)
657 self.assertRaises(TypeError, mock)
658 mock(1, 2)
659 mock.assert_called_with(1, 2)
660
661 f.f = f
662 mock = create_autospec(f)
663 self.assertRaises(TypeError, mock.f)
664 mock.f(3, 4)
665 mock.f.assert_called_with(3, 4)
666
667
668 def test_signature_class(self):
669 class Foo(object):
670 def __init__(self, a, b=3):
671 pass
672
673 mock = create_autospec(Foo)
674
675 self.assertRaises(TypeError, mock)
676 mock(1)
677 mock.assert_called_once_with(1)
678
679 mock(4, 5)
680 mock.assert_called_with(4, 5)
681
682
683 @unittest2.skipIf(inPy3k, 'no old style classes in Python 3')
684 def test_signature_old_style_class(self):
685 class Foo:
686 def __init__(self, a, b=3):
687 pass
688
689 mock = create_autospec(Foo)
690
691 self.assertRaises(TypeError, mock)
692 mock(1)
693 mock.assert_called_once_with(1)
694
695 mock(4, 5)
696 mock.assert_called_with(4, 5)
697
698
699 def test_class_with_no_init(self):
700 # this used to raise an exception
701 # due to trying to get a signature from object.__init__
702 class Foo(object):
703 pass
704 create_autospec(Foo)
705
706
707 @unittest2.skipIf(inPy3k, 'no old style classes in Python 3')
708 def test_old_style_class_with_no_init(self):
709 # this used to raise an exception
710 # due to Foo.__init__ raising an AttributeError
711 class Foo:
712 pass
713 create_autospec(Foo)
714
715
716 def test_signature_callable(self):
717 class Callable(object):
718 def __init__(self):
719 pass
720 def __call__(self, a):
721 pass
722
723 mock = create_autospec(Callable)
724 mock()
725 mock.assert_called_once_with()
726 self.assertRaises(TypeError, mock, 'a')
727
728 instance = mock()
729 self.assertRaises(TypeError, instance)
730 instance(a='a')
731 instance.assert_called_once_with(a='a')
732 instance('a')
733 instance.assert_called_with('a')
734
735 mock = create_autospec(Callable())
736 mock(a='a')
737 mock.assert_called_once_with(a='a')
738 self.assertRaises(TypeError, mock)
739 mock('a')
740 mock.assert_called_with('a')
741
742
743 def test_signature_noncallable(self):
744 class NonCallable(object):
745 def __init__(self):
746 pass
747
748 mock = create_autospec(NonCallable)
749 instance = mock()
750 mock.assert_called_once_with()
751 self.assertRaises(TypeError, mock, 'a')
752 self.assertRaises(TypeError, instance)
753 self.assertRaises(TypeError, instance, 'a')
754
755 mock = create_autospec(NonCallable())
756 self.assertRaises(TypeError, mock)
757 self.assertRaises(TypeError, mock, 'a')
758
759
760 def test_create_autospec_none(self):
761 class Foo(object):
762 bar = None
763
764 mock = create_autospec(Foo)
765 none = mock.bar
766 self.assertNotIsInstance(none, type(None))
767
768 none.foo()
769 none.foo.assert_called_once_with()
770
771
772 def test_autospec_functions_with_self_in_odd_place(self):
773 class Foo(object):
774 def f(a, self):
775 pass
776
777 a = create_autospec(Foo)
778 a.f(self=10)
779 a.f.assert_called_with(self=10)
780
781
782 def test_autospec_property(self):
783 class Foo(object):
784 @property
785 def foo(self):
786 return 3
787
788 foo = create_autospec(Foo)
789 mock_property = foo.foo
790
791 # no spec on properties
792 self.assertTrue(isinstance(mock_property, MagicMock))
793 mock_property(1, 2, 3)
794 mock_property.abc(4, 5, 6)
795 mock_property.assert_called_once_with(1, 2, 3)
796 mock_property.abc.assert_called_once_with(4, 5, 6)
797
798
799 def test_autospec_slots(self):
800 class Foo(object):
801 __slots__ = ['a']
802
803 foo = create_autospec(Foo)
804 mock_slot = foo.a
805
806 # no spec on slots
807 mock_slot(1, 2, 3)
808 mock_slot.abc(4, 5, 6)
809 mock_slot.assert_called_once_with(1, 2, 3)
810 mock_slot.abc.assert_called_once_with(4, 5, 6)
811
812
813class TestCallList(unittest2.TestCase):
814
815 def test_args_list_contains_call_list(self):
Michael Foorda9a8bb92012-03-13 14:39:59 -0700816 mock = Mock()
817 self.assertIsInstance(mock.call_args_list, _CallList)
Michael Foord1e68bec2012-03-03 22:24:30 +0000818
Michael Foorda9a8bb92012-03-13 14:39:59 -0700819 mock(1, 2)
820 mock(a=3)
821 mock(3, 4)
822 mock(b=6)
Michael Foord1e68bec2012-03-03 22:24:30 +0000823
Michael Foorda9a8bb92012-03-13 14:39:59 -0700824 for kall in call(1, 2), call(a=3), call(3, 4), call(b=6):
825 self.assertTrue(kall in mock.call_args_list)
Michael Foord1e68bec2012-03-03 22:24:30 +0000826
Michael Foorda9a8bb92012-03-13 14:39:59 -0700827 calls = [call(a=3), call(3, 4)]
828 self.assertTrue(calls in mock.call_args_list)
829 calls = [call(1, 2), call(a=3)]
830 self.assertTrue(calls in mock.call_args_list)
831 calls = [call(3, 4), call(b=6)]
832 self.assertTrue(calls in mock.call_args_list)
833 calls = [call(3, 4)]
834 self.assertTrue(calls in mock.call_args_list)
Michael Foord1e68bec2012-03-03 22:24:30 +0000835
Michael Foorda9a8bb92012-03-13 14:39:59 -0700836 self.assertFalse(call('fish') in mock.call_args_list)
837 self.assertFalse([call('fish')] in mock.call_args_list)
Michael Foord1e68bec2012-03-03 22:24:30 +0000838
839
840 def test_call_list_str(self):
841 mock = Mock()
842 mock(1, 2)
843 mock.foo(a=3)
844 mock.foo.bar().baz('fish', cat='dog')
845
846 expected = (
847 "[call(1, 2),\n"
848 " call.foo(a=3),\n"
849 " call.foo.bar(),\n"
850 " call.foo.bar().baz('fish', cat='dog')]"
851 )
852 self.assertEqual(str(mock.mock_calls), expected)
853
854
855if __name__ == '__main__':
856 unittest2.main()