4
4
5
5
6
6
class MockFixture (object ):
7
-
8
7
def __init__ (self ):
9
8
self ._patches = []
10
9
self .patch = self ._Patcher (self ._patches )
@@ -27,6 +26,15 @@ def object(self, *args, **kwargs):
27
26
return mocked
28
27
29
28
29
+ def multiple (self , * args , ** kwargs ):
30
+ import mock
31
+
32
+ p = mock .patch .multiple (* args , ** kwargs )
33
+ mocked = p .start ()
34
+ self ._patches .append (p )
35
+ return mocked
36
+
37
+
30
38
def __call__ (self , * args , ** kwargs ):
31
39
import mock
32
40
@@ -56,13 +64,23 @@ def ls(cls, path):
56
64
@pytest .fixture
57
65
def check_unix_fs_mocked (tmpdir , mock ):
58
66
def check (mocked_rm , mocked_ls ):
67
+ assert mocked_rm is os .remove
68
+ assert mocked_ls is os .listdir
69
+
59
70
file_name = tmpdir / 'foo.txt'
60
71
file_name .ensure ()
72
+
61
73
UnixFS .rm (str (file_name ))
62
- os .remove .assert_called_once_with (str (file_name ))
63
74
mocked_rm .assert_called_once_with (str (file_name ))
64
75
assert os .path .isfile (str (file_name ))
76
+
77
+ mocked_ls .return_value = ['bar.txt' ]
78
+ assert UnixFS .ls (str (tmpdir )) == ['bar.txt' ]
79
+ mocked_ls .assert_called_once_with (str (tmpdir ))
80
+
65
81
mock .stopall ()
82
+
83
+ assert UnixFS .ls (str (tmpdir )) == ['foo.txt' ]
66
84
UnixFS .rm (str (file_name ))
67
85
assert not os .path .isfile (str (file_name ))
68
86
@@ -77,7 +95,15 @@ def mock_using_patch(mock):
77
95
return mock .patch ('os.remove' ), mock .patch ('os.listdir' )
78
96
79
97
80
- @pytest .mark .parametrize ('mock_fs' , [mock_using_patch_object , mock_using_patch ],
98
+ def mock_using_patch_multiple (mock ):
99
+ from mock import DEFAULT
100
+
101
+ r = mock .patch .multiple ('os' , remove = DEFAULT , listdir = DEFAULT )
102
+ return r ['remove' ], r ['listdir' ]
103
+
104
+
105
+ @pytest .mark .parametrize ('mock_fs' , [mock_using_patch_object , mock_using_patch ,
106
+ mock_using_patch_multiple ],
81
107
)
82
108
def test_fixture (mock_fs , mock , check_unix_fs_mocked ):
83
109
mocked_rm , mocked_ls = mock_fs (mock )
0 commit comments