-
Notifications
You must be signed in to change notification settings - Fork 16
/
flexmock_test.py
1832 lines (1518 loc) · 63.7 KB
/
flexmock_test.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from flexmock import EXACTLY
from flexmock import AT_LEAST
from flexmock import AT_MOST
from flexmock import UPDATED_ATTRS
from flexmock import Mock
from flexmock import MockBuiltinError
from flexmock import FlexmockContainer
from flexmock import FlexmockError
from flexmock import MethodSignatureError
from flexmock import ExceptionClassError
from flexmock import ExceptionMessageError
from flexmock import StateError
from flexmock import MethodCallError
from flexmock import CallOrderError
from flexmock import ReturnValue
from flexmock import flexmock_teardown
from flexmock import _format_args
from flexmock import _isproperty
import flexmock
import re
import sys
import unittest
import some_module
def module_level_function(some, args):
return "%s, %s" % (some, args)
module_level_attribute = 'test'
class OldStyleClass:
pass
class NewStyleClass(object):
pass
def assertRaises(exception, method, *kargs, **kwargs):
try:
method(*kargs, **kwargs)
except exception:
return
except:
instance = sys.exc_info()[1]
print('%s' % instance)
raise Exception('%s not raised' % exception.__name__)
def assertEqual(expected, received, msg=''):
if not msg:
msg = 'expected %s, received %s' % (expected, received)
if expected != received:
raise AssertionError('%s != %s : %s' % (expected, received, msg))
class RegularClass(object):
def _tear_down(self):
return flexmock_teardown()
def test_flexmock_should_create_mock_object(self):
mock = flexmock()
assert isinstance(mock, Mock)
def test_flexmock_should_create_mock_object_from_dict(self):
mock = flexmock(foo='foo', bar='bar')
assertEqual('foo', mock.foo)
assertEqual('bar', mock.bar)
def test_flexmock_should_add_expectations(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo')
assert ('method_foo' in
[x.name for x in FlexmockContainer.flexmock_objects[mock]])
def test_flexmock_should_return_value(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar')
mock.should_receive('method_bar').and_return('value_baz')
assertEqual('value_bar', mock.method_foo())
assertEqual('value_baz', mock.method_bar())
def test_type_flexmock_with_unicode_string_in_should_receive(self):
class Foo(object):
def bar(self): return 'bar'
flexmock(Foo).should_receive(u'bar').and_return('mocked_bar')
foo = Foo()
assertEqual('mocked_bar', foo.bar())
def test_flexmock_should_accept_shortcuts_for_creating_mock_object(self):
mock = flexmock(attr1='value 1', attr2=lambda: 'returning 2')
assertEqual('value 1', mock.attr1)
assertEqual('returning 2', mock.attr2())
def test_flexmock_should_accept_shortcuts_for_creating_expectations(self):
class Foo:
def method1(self):
pass
def method2(self):
pass
foo = Foo()
flexmock(foo, method1='returning 1', method2='returning 2')
assertEqual('returning 1', foo.method1())
assertEqual('returning 2', foo.method2())
assertEqual('returning 2', foo.method2())
def test_flexmock_expectations_returns_all(self):
mock = flexmock(name='temp')
assert mock not in FlexmockContainer.flexmock_objects
mock.should_receive('method_foo')
mock.should_receive('method_bar')
assertEqual(2, len(FlexmockContainer.flexmock_objects[mock]))
def test_flexmock_expectations_returns_named_expectation(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo')
assertEqual('method_foo',
FlexmockContainer.get_flexmock_expectation(mock, 'method_foo').name)
def test_flexmock_expectations_returns_none_if_not_found(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo')
assert (FlexmockContainer.get_flexmock_expectation(mock, 'method_bar') is None)
def test_flexmock_should_check_parameters(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').with_args('bar').and_return(1)
mock.should_receive('method_foo').with_args('baz').and_return(2)
assertEqual(1, mock.method_foo('bar'))
assertEqual(2, mock.method_foo('baz'))
def test_flexmock_should_keep_track_of_calls(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').with_args('foo').and_return(0)
mock.should_receive('method_foo').with_args('bar').and_return(1)
mock.should_receive('method_foo').with_args('baz').and_return(2)
mock.method_foo('bar')
mock.method_foo('bar')
mock.method_foo('baz')
expectation = FlexmockContainer.get_flexmock_expectation(
mock, 'method_foo', ('foo',))
assertEqual(0, expectation.times_called)
expectation = FlexmockContainer.get_flexmock_expectation(
mock, 'method_foo', ('bar',))
assertEqual(2, expectation.times_called)
expectation = FlexmockContainer.get_flexmock_expectation(
mock, 'method_foo', ('baz',))
assertEqual(1, expectation.times_called)
def test_flexmock_should_set_expectation_call_numbers(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').times(1)
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertRaises(MethodCallError, expectation.verify)
mock.method_foo()
expectation.verify()
def test_flexmock_should_check_raised_exceptions(self):
mock = flexmock(name='temp')
class FakeException(Exception):
pass
mock.should_receive('method_foo').and_raise(FakeException)
assertRaises(FakeException, mock.method_foo)
assertEqual(1,
FlexmockContainer.get_flexmock_expectation(
mock, 'method_foo').times_called)
def test_flexmock_should_check_raised_exceptions_instance_with_args(self):
mock = flexmock(name='temp')
class FakeException(Exception):
def __init__(self, arg, arg2):
pass
mock.should_receive('method_foo').and_raise(FakeException(1, arg2=2))
assertRaises(FakeException, mock.method_foo)
assertEqual(1,
FlexmockContainer.get_flexmock_expectation(
mock, 'method_foo').times_called)
def test_flexmock_should_check_raised_exceptions_class_with_args(self):
mock = flexmock(name='temp')
class FakeException(Exception):
def __init__(self, arg, arg2):
pass
mock.should_receive('method_foo').and_raise(FakeException, 1, arg2=2)
assertRaises(FakeException, mock.method_foo)
assertEqual(1,
FlexmockContainer.get_flexmock_expectation(
mock, 'method_foo').times_called)
def test_flexmock_should_match_any_args_by_default(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('bar')
mock.should_receive('method_foo').with_args('baz').and_return('baz')
assertEqual('bar', mock.method_foo())
assertEqual('bar', mock.method_foo(1))
assertEqual('bar', mock.method_foo('foo', 'bar'))
assertEqual('baz', mock.method_foo('baz'))
def test_flexmock_should_fail_to_match_exactly_no_args_when_calling_with_args(self):
mock = flexmock()
mock.should_receive('method_foo').with_args()
assertRaises(MethodSignatureError, mock.method_foo, 'baz')
def test_flexmock_should_match_exactly_no_args(self):
class Foo:
def bar(self): pass
foo = Foo()
flexmock(foo).should_receive('bar').with_args().and_return('baz')
assertEqual('baz', foo.bar())
def test_expectation_dot_mock_should_return_mock(self):
mock = flexmock(name='temp')
assertEqual(mock, mock.should_receive('method_foo').mock)
def test_flexmock_should_create_partial_new_style_object_mock(self):
class User(object):
def __init__(self, name=None):
self.name = name
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
user = User()
flexmock(user)
user.should_receive('get_name').and_return('john')
user.set_name('mike')
assertEqual('john', user.get_name())
def test_flexmock_should_create_partial_old_style_object_mock(self):
class User:
def __init__(self, name=None):
self.name = name
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
user = User()
flexmock(user)
user.should_receive('get_name').and_return('john')
user.set_name('mike')
assertEqual('john', user.get_name())
def test_flexmock_should_create_partial_new_style_class_mock(self):
class User(object):
def __init__(self):
pass
def get_name(self):
pass
flexmock(User)
User.should_receive('get_name').and_return('mike')
user = User()
assertEqual('mike', user.get_name())
def test_flexmock_should_create_partial_old_style_class_mock(self):
class User:
def __init__(self):
pass
def get_name(self):
pass
flexmock(User)
User.should_receive('get_name').and_return('mike')
user = User()
assertEqual('mike', user.get_name())
def test_flexmock_should_match_expectations_against_builtin_classes(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').with_args(str).and_return('got a string')
mock.should_receive('method_foo').with_args(int).and_return('got an int')
assertEqual('got a string', mock.method_foo('string!'))
assertEqual('got an int', mock.method_foo(23))
assertRaises(MethodSignatureError, mock.method_foo, 2.0)
def test_flexmock_should_match_expectations_against_user_defined_classes(self):
mock = flexmock(name='temp')
class Foo:
pass
mock.should_receive('method_foo').with_args(Foo).and_return('got a Foo')
assertEqual('got a Foo', mock.method_foo(Foo()))
assertRaises(MethodSignatureError, mock.method_foo, 1)
def test_flexmock_configures_global_mocks_dict(self):
mock = flexmock(name='temp')
assert mock not in FlexmockContainer.flexmock_objects
mock.should_receive('method_foo')
assert mock in FlexmockContainer.flexmock_objects
assertEqual(len(FlexmockContainer.flexmock_objects[mock]), 1)
def test_flexmock_teardown_verifies_mocks(self):
mock = flexmock(name='temp')
mock.should_receive('verify_expectations').times(1)
assertRaises(MethodCallError, self._tear_down)
def test_flexmock_teardown_does_not_verify_stubs(self):
mock = flexmock(name='temp')
mock.should_receive('verify_expectations')
self._tear_down()
def test_flexmock_preserves_stubbed_object_methods_between_tests(self):
class User:
def get_name(self):
return 'mike'
user = User()
flexmock(user).should_receive('get_name').and_return('john')
assertEqual('john', user.get_name())
self._tear_down()
assertEqual('mike', user.get_name())
def test_flexmock_preserves_stubbed_class_methods_between_tests(self):
class User:
def get_name(self):
return 'mike'
user = User()
flexmock(User).should_receive('get_name').and_return('john')
assertEqual('john', user.get_name())
self._tear_down()
assertEqual('mike', user.get_name())
def test_flexmock_removes_new_stubs_from_objects_after_tests(self):
class User:
def get_name(self): pass
user = User()
saved = user.get_name
flexmock(user).should_receive('get_name').and_return('john')
assert saved != user.get_name
assertEqual('john', user.get_name())
self._tear_down()
assertEqual(saved, user.get_name)
def test_flexmock_removes_new_stubs_from_classes_after_tests(self):
class User:
def get_name(self): pass
user = User()
saved = user.get_name
flexmock(User).should_receive('get_name').and_return('john')
assert saved != user.get_name
assertEqual('john', user.get_name())
self._tear_down()
assertEqual(saved, user.get_name)
def test_flexmock_removes_stubs_from_multiple_objects_on_teardown(self):
class User:
def get_name(self): pass
class Group:
def get_name(self): pass
user = User()
group = User()
saved1 = user.get_name
saved2 = group.get_name
flexmock(user).should_receive('get_name').and_return('john').once()
flexmock(group).should_receive('get_name').and_return('john').once()
assert saved1 != user.get_name
assert saved2 != group.get_name
assertEqual('john', user.get_name())
assertEqual('john', group.get_name())
self._tear_down()
assertEqual(saved1, user.get_name)
assertEqual(saved2, group.get_name)
def test_flexmock_removes_stubs_from_multiple_classes_on_teardown(self):
class User:
def get_name(self): pass
class Group:
def get_name(self): pass
user = User()
group = User()
saved1 = user.get_name
saved2 = group.get_name
flexmock(User).should_receive('get_name').and_return('john')
flexmock(Group).should_receive('get_name').and_return('john')
assert saved1 != user.get_name
assert saved2 != group.get_name
assertEqual('john', user.get_name())
assertEqual('john', group.get_name())
self._tear_down()
assertEqual(saved1, user.get_name)
assertEqual(saved2, group.get_name)
def test_flexmock_respects_at_least_when_called_less_than_requested(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('bar').at_least().twice()
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(AT_LEAST, expectation.modifier)
mock.method_foo()
assertRaises(MethodCallError, self._tear_down)
def test_flexmock_respects_at_least_when_called_requested_number(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar').at_least().once()
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(AT_LEAST, expectation.modifier)
mock.method_foo()
self._tear_down()
def test_flexmock_respects_at_least_when_called_more_than_requested(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar').at_least().once()
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(AT_LEAST, expectation.modifier)
mock.method_foo()
mock.method_foo()
self._tear_down()
def test_flexmock_respects_at_most_when_called_less_than_requested(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('bar').at_most().twice()
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(AT_MOST, expectation.modifier)
mock.method_foo()
self._tear_down()
def test_flexmock_respects_at_most_when_called_requested_number(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar').at_most().once()
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(AT_MOST, expectation.modifier)
mock.method_foo()
self._tear_down()
def test_flexmock_respects_at_most_when_called_more_than_requested(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar').at_most().once()
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(AT_MOST, expectation.modifier)
mock.method_foo()
assertRaises(MethodCallError, mock.method_foo)
def test_flexmock_treats_once_as_times_one(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar').once()
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(1, expectation.expected_calls[EXACTLY])
assertRaises(MethodCallError, self._tear_down)
def test_flexmock_treats_twice_as_times_two(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').twice().and_return('value_bar')
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(2, expectation.expected_calls[EXACTLY])
assertRaises(MethodCallError, self._tear_down)
def test_flexmock_works_with_never_when_true(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar').never
expectation = FlexmockContainer.get_flexmock_expectation(mock, 'method_foo')
assertEqual(0, expectation.expected_calls[EXACTLY])
self._tear_down()
def test_flexmock_works_with_never_when_false(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').and_return('value_bar').never
assertRaises(MethodCallError, mock.method_foo)
def test_flexmock_get_flexmock_expectation_should_work_with_args(self):
mock = flexmock(name='temp')
mock.should_receive('method_foo').with_args('value_bar')
assert FlexmockContainer.get_flexmock_expectation(
mock, 'method_foo', 'value_bar')
def test_flexmock_function_should_return_previously_mocked_object(self):
class User(object):
pass
user = User()
foo = flexmock(user)
assert foo == user
assert foo == flexmock(user)
def test_flexmock_should_not_return_class_object_if_mocking_instance(self):
class User:
def method(self):
pass
user = User()
user2 = User()
class_mock = flexmock(User).should_receive(
'method').and_return('class').mock
user_mock = flexmock(user).should_receive(
'method').and_return('instance').mock
assert class_mock is not user_mock
assertEqual('instance', user.method())
assertEqual('class', user2.method())
def test_should_call_on_class_mock(self):
class User:
def foo(self): return 'class'
user1 = User()
user2 = User()
flexmock(User).should_call('foo').once()
assertRaises(MethodCallError, self._tear_down)
flexmock(User).should_call('foo').twice()
assertEqual('class', user1.foo())
assertEqual('class', user2.foo())
def test_flexmock_should_not_blow_up_on_should_call_for_class_methods(self):
class User:
@classmethod
def foo(self):
return 'class'
flexmock(User).should_call('foo')
assertEqual('class', User.foo())
def test_flexmock_should_not_blow_up_on_should_call_for_static_methods(self):
class User:
@staticmethod
def foo():
return 'static'
flexmock(User).should_call('foo')
assertEqual('static', User.foo())
def test_flexmock_should_mock_new_instances_with_multiple_params(self):
class User(object):
pass
class Group(object):
def __init__(self, arg, arg2):
pass
user = User()
flexmock(Group).new_instances(user)
assert user is Group(1, 2)
def test_flexmock_should_revert_new_instances_on_teardown(self):
class User(object):
pass
class Group(object):
pass
user = User()
group = Group()
flexmock(Group).new_instances(user)
assert user is Group()
self._tear_down()
assertEqual(group.__class__, Group().__class__)
def test_flexmock_should_cleanup_added_methods_and_attributes(self):
class Group(object):
pass
group = Group()
flexmock(Group)
assert 'should_receive' in Group.__dict__
assert 'should_receive' not in group.__dict__
flexmock(group)
assert 'should_receive' in group.__dict__
self._tear_down()
for method in UPDATED_ATTRS:
assert method not in Group.__dict__
assert method not in group.__dict__
def test_flexmock_should_cleanup_after_exception(self):
class User:
def method2(self):
pass
class Group:
def method1(self):
pass
flexmock(Group)
flexmock(User)
Group.should_receive('method1').once()
User.should_receive('method2').once()
assertRaises(MethodCallError, self._tear_down)
for method in UPDATED_ATTRS:
assert method not in dir(Group)
for method in UPDATED_ATTRS:
assert method not in dir(User)
def test_flexmock_should_call_respects_matched_expectations(self):
class Group(object):
def method1(self, arg1, arg2='b'):
return '%s:%s' % (arg1, arg2)
def method2(self, arg):
return arg
group = Group()
flexmock(group).should_call('method1').twice()
assertEqual('a:c', group.method1('a', arg2='c'))
assertEqual('a:b', group.method1('a'))
group.should_call('method2').once().with_args('c')
assertEqual('c', group.method2('c'))
self._tear_down()
def test_flexmock_should_call_respects_unmatched_expectations(self):
class Group(object):
def method1(self, arg1, arg2='b'):
return '%s:%s' % (arg1, arg2)
def method2(self, a):
pass
group = Group()
flexmock(group).should_call('method1').at_least().once()
assertRaises(MethodCallError, self._tear_down)
flexmock(group)
group.should_call('method2').with_args('a').once()
group.should_receive('method2').with_args('not a')
group.method2('not a')
assertRaises(MethodCallError, self._tear_down)
def test_flexmock_doesnt_error_on_properly_ordered_expectations(self):
class Foo(object):
def foo(self):
pass
def method1(self, a):
pass
def bar(self):
pass
def baz(self):
pass
foo = Foo()
flexmock(foo).should_receive('foo')
flexmock(foo).should_receive('method1').with_args('a').ordered()
flexmock(foo).should_receive('bar')
flexmock(foo).should_receive('method1').with_args('b').ordered()
flexmock(foo).should_receive('baz')
foo.bar()
foo.method1('a')
foo.method1('b')
foo.baz()
foo.foo()
def test_flexmock_errors_on_improperly_ordered_expectations(self):
class Foo(object):
def method1(self, a): pass
foo = Foo()
flexmock(foo)
foo.should_receive('method1').with_args('a').ordered()
foo.should_receive('method1').with_args('b').ordered()
assertRaises(CallOrderError, foo.method1, 'b')
def test_flexmock_should_accept_multiple_return_values(self):
class Foo:
def method1(self): pass
foo = Foo()
flexmock(foo).should_receive('method1').and_return(1, 5).and_return(2)
assertEqual((1, 5), foo.method1())
assertEqual(2, foo.method1())
assertEqual((1, 5), foo.method1())
assertEqual(2, foo.method1())
def test_flexmock_should_accept_multiple_return_values_with_shortcut(self):
class Foo:
def method1(self): pass
foo = Foo()
flexmock(foo).should_receive('method1').and_return(1, 2).one_by_one()
assertEqual(1, foo.method1())
assertEqual(2, foo.method1())
assertEqual(1, foo.method1())
assertEqual(2, foo.method1())
def test_flexmock_should_mix_multiple_return_values_with_exceptions(self):
class Foo:
def method1(self): pass
foo = Foo()
flexmock(foo).should_receive('method1').and_return(1).and_raise(Exception)
assertEqual(1, foo.method1())
assertRaises(Exception, foo.method1)
assertEqual(1, foo.method1())
assertRaises(Exception, foo.method1)
def test_flexmock_should_match_types_on_multiple_arguments(self):
class Foo:
def method1(self, a, b): pass
foo = Foo()
flexmock(foo).should_receive('method1').with_args(str, int).and_return('ok')
assertEqual('ok', foo.method1('some string', 12))
assertRaises(MethodSignatureError, foo.method1, 12, 32)
flexmock(foo).should_receive('method1').with_args(str, int).and_return('ok')
assertRaises(MethodSignatureError, foo.method1, 12, 'some string')
flexmock(foo).should_receive('method1').with_args(str, int).and_return('ok')
assertRaises(MethodSignatureError, foo.method1, 'string', 12, 14)
def test_flexmock_should_match_types_on_multiple_arguments_generic(self):
class Foo:
def method1(self, a, b, c): pass
foo = Foo()
flexmock(foo).should_receive('method1').with_args(
object, object, object).and_return('ok')
assertEqual('ok', foo.method1('some string', None, 12))
assertEqual('ok', foo.method1((1,), None, 12))
assertEqual('ok', foo.method1(12, 14, []))
assertEqual('ok', foo.method1('some string', 'another one', False))
assertRaises(MethodSignatureError, foo.method1, 'string', 12)
flexmock(foo).should_receive('method1').with_args(
object, object, object).and_return('ok')
assertRaises(MethodSignatureError, foo.method1, 'string', 12, 13, 14)
def test_flexmock_should_match_types_on_multiple_arguments_classes(self):
class Foo:
def method1(self, a, b):
pass
class Bar:
pass
foo = Foo()
bar = Bar()
flexmock(foo).should_receive('method1').with_args(
object, Bar).and_return('ok')
assertEqual('ok', foo.method1('some string', bar))
assertRaises(MethodSignatureError, foo.method1, bar, 'some string')
flexmock(foo).should_receive('method1').with_args(
object, Bar).and_return('ok')
assertRaises(MethodSignatureError, foo.method1, 12, 'some string')
def test_flexmock_should_match_keyword_arguments(self):
class Foo:
def method1(self, a, **kwargs): pass
foo = Foo()
flexmock(foo).should_receive('method1').with_args(1, arg3=3, arg2=2).twice()
foo.method1(1, arg2=2, arg3=3)
foo.method1(1, arg3=3, arg2=2)
self._tear_down()
flexmock(foo).should_receive('method1').with_args(1, arg3=3, arg2=2)
assertRaises(MethodSignatureError, foo.method1, arg2=2, arg3=3)
flexmock(foo).should_receive('method1').with_args(1, arg3=3, arg2=2)
assertRaises(MethodSignatureError, foo.method1, 1, arg2=2, arg3=4)
flexmock(foo).should_receive('method1').with_args(1, arg3=3, arg2=2)
assertRaises(MethodSignatureError, foo.method1, 1)
def test_flexmock_should_call_should_match_keyword_arguments(self):
class Foo:
def method1(self, arg1, arg2=None, arg3=None):
return '%s%s%s' % (arg1, arg2, arg3)
foo = Foo()
flexmock(foo).should_call('method1').with_args(1, arg3=3, arg2=2).once()
assertEqual('123', foo.method1(1, arg2=2, arg3=3))
def test_flexmock_should_mock_private_methods(self):
class Foo:
def __private_method(self):
return 'foo'
def public_method(self):
return self.__private_method()
foo = Foo()
flexmock(foo).should_receive('__private_method').and_return('bar')
assertEqual('bar', foo.public_method())
def test_flexmock_should_mock_special_methods(self):
class Foo:
def __special_method__(self):
return 'foo'
def public_method(self):
return self.__special_method__()
foo = Foo()
flexmock(foo).should_receive('__special_method__').and_return('bar')
assertEqual('bar', foo.public_method())
def test_flexmock_should_mock_double_underscore_method(self):
class Foo:
def __(self):
return 'foo'
def public_method(self):
return self.__()
foo = Foo()
flexmock(foo).should_receive('__').and_return('bar')
assertEqual('bar', foo.public_method())
def test_flexmock_should_mock_private_class_methods(self):
class Foo:
def __iter__(self): pass
flexmock(Foo).should_receive('__iter__').and_yield(1, 2, 3)
assertEqual([1, 2, 3], [x for x in Foo()])
def test_flexmock_should_mock_iter_on_new_style_instances(self):
class Foo(object):
def __iter__(self):
yield None
old = Foo.__iter__
foo = Foo()
foo2 = Foo()
foo3 = Foo()
flexmock(foo, __iter__=iter([1, 2, 3]))
flexmock(foo2, __iter__=iter([3, 4, 5]))
assertEqual([1, 2, 3], [x for x in foo])
assertEqual([3, 4, 5], [x for x in foo2])
assertEqual([None], [x for x in foo3])
assertEqual(False, foo.__iter__ == old)
assertEqual(False, foo2.__iter__ == old)
assertEqual(False, foo3.__iter__ == old)
self._tear_down()
assertEqual([None], [x for x in foo])
assertEqual([None], [x for x in foo2])
assertEqual([None], [x for x in foo3])
assertEqual(True, Foo.__iter__ == old, '%s != %s' % (Foo.__iter__, old))
def test_flexmock_should_mock_private_methods_with_leading_underscores(self):
class _Foo:
def __stuff(self):
pass
def public_method(self):
return self.__stuff()
foo = _Foo()
flexmock(foo).should_receive('__stuff').and_return('bar')
assertEqual('bar', foo.public_method())
def test_flexmock_should_mock_generators(self):
class Gen:
def foo(self):
pass
gen = Gen()
flexmock(gen).should_receive('foo').and_yield(*range(1, 10))
output = [val for val in gen.foo()]
assertEqual([val for val in range(1, 10)], output)
def test_flexmock_should_verify_correct_spy_return_values(self):
class User:
def get_stuff(self): return 'real', 'stuff'
user = User()
flexmock(user).should_call('get_stuff').and_return('real', 'stuff')
assertEqual(('real', 'stuff'), user.get_stuff())
def test_flexmock_should_verify_correct_spy_regexp_return_values(self):
class User:
def get_stuff(self): return 'real', 'stuff'
user = User()
flexmock(user).should_call('get_stuff').and_return(
re.compile('ea.*'), re.compile('^stuff$'))
assertEqual(('real', 'stuff'), user.get_stuff())
def test_flexmock_should_verify_spy_raises_correct_exception_class(self):
class FakeException(Exception):
def __init__(self, param, param2):
self.message = '%s, %s' % (param, param2)
Exception.__init__(self)
class User:
def get_stuff(self): raise FakeException(1, 2)
user = User()
flexmock(user).should_call('get_stuff').and_raise(FakeException, 1, 2)
user.get_stuff()
def test_flexmock_should_verify_spy_matches_exception_message(self):
class FakeException(Exception):
def __init__(self, param, param2):
self.p1 = param
self.p2 = param2
Exception.__init__(self, param)
def __str__(self):
return '%s, %s' % (self.p1, self.p2)
class User:
def get_stuff(self): raise FakeException('1', '2')
user = User()
flexmock(user).should_call('get_stuff').and_raise(FakeException, '2', '1')
assertRaises(ExceptionMessageError, user.get_stuff)
def test_flexmock_should_verify_spy_matches_exception_regexp(self):
class User:
def get_stuff(self): raise Exception('123asdf345')
user = User()
flexmock(user).should_call(
'get_stuff').and_raise(Exception, re.compile('asdf'))
user.get_stuff()
self._tear_down()
def test_flexmock_should_verify_spy_matches_exception_regexp_mismatch(self):
class User:
def get_stuff(self): raise Exception('123asdf345')
user = User()
flexmock(user).should_call(
'get_stuff').and_raise(Exception, re.compile('^asdf'))
assertRaises(ExceptionMessageError, user.get_stuff)
def test_flexmock_should_blow_up_on_wrong_spy_exception_type(self):
class User:
def get_stuff(self): raise CallOrderError('foo')
user = User()
flexmock(user).should_call('get_stuff').and_raise(MethodCallError)
assertRaises(ExceptionClassError, user.get_stuff)
def test_flexmock_should_match_spy_exception_parent_type(self):
class User:
def get_stuff(self): raise CallOrderError('foo')
user = User()
flexmock(user).should_call('get_stuff').and_raise(FlexmockError)
user.get_stuff()
def test_flexmock_should_blow_up_on_wrong_spy_return_values(self):
class User:
def get_stuff(self):
return 'real', 'stuff'
def get_more_stuff(self):
return 'other', 'stuff'
user = User()
flexmock(user).should_call('get_stuff').and_return('other', 'stuff')
assertRaises(MethodSignatureError, user.get_stuff)
flexmock(user).should_call('get_more_stuff').and_return()
assertRaises(MethodSignatureError, user.get_more_stuff)
def test_flexmock_should_mock_same_class_twice(self):
class Foo:
pass
flexmock(Foo)
flexmock(Foo)
def test_flexmock_spy_should_not_clobber_original_method(self):
class User:
def get_stuff(self): return 'real', 'stuff'
user = User()
flexmock(user).should_call('get_stuff')
flexmock(user).should_call('get_stuff')
assertEqual(('real', 'stuff'), user.get_stuff())
def test_flexmock_should_properly_restore_static_methods(self):
class User:
@staticmethod
def get_stuff(): return 'ok!'
assertEqual('ok!', User.get_stuff())
flexmock(User).should_receive('get_stuff')
assert User.get_stuff() is None
self._tear_down()
assertEqual('ok!', User.get_stuff())
def test_flexmock_should_properly_restore_undecorated_static_methods(self):
class User:
def get_stuff(): return 'ok!'
get_stuff = staticmethod(get_stuff)
assertEqual('ok!', User.get_stuff())
flexmock(User).should_receive('get_stuff')
assert User.get_stuff() is None
self._tear_down()
assertEqual('ok!', User.get_stuff())
def test_flexmock_should_properly_restore_module_level_functions(self):
if 'flexmock_test' in sys.modules:
mod = sys.modules['flexmock_test']
else:
mod = sys.modules['__main__']
flexmock(mod).should_receive('module_level_function').with_args(1, 2)
assertEqual(None, module_level_function(1, 2))
self._tear_down()
assertEqual('1, 2', module_level_function(1, 2))
def test_module_level_function_with_kwargs(self):
if 'flexmock_test' in sys.modules:
mod = sys.modules['flexmock_test']
else:
mod = sys.modules['__main__']
flexmock(mod).should_receive('module_level_function').with_args(
1, args="expected")
assertRaises(FlexmockError, module_level_function, 1, args="not expected")
def test_flexmock_should_support_mocking_old_style_classes_as_functions(self):
if 'flexmock_test' in sys.modules:
mod = sys.modules['flexmock_test']