@@ -7,26 +7,35 @@ class MockFixture(object):
7
7
8
8
def __init__ (self ):
9
9
self ._patches = []
10
- self .patch = self ._PatchWrapper (self ._patches )
10
+ self .patch = self ._Patcher (self ._patches )
11
11
12
12
def stopall (self ):
13
13
for p in self ._patches :
14
14
p .stop ()
15
15
self ._patches [:] = []
16
16
17
- class _PatchWrapper (object ):
18
-
17
+ class _Patcher (object ):
19
18
def __init__ (self , patches ):
20
19
self ._patches = patches
21
20
22
21
def object (self , * args , ** kwargs ):
23
22
import mock
23
+
24
24
p = mock .patch .object (* args , ** kwargs )
25
25
mocked = p .start ()
26
26
self ._patches .append (p )
27
27
return mocked
28
28
29
29
30
+ def __call__ (self , * args , ** kwargs ):
31
+ import mock
32
+
33
+ p = mock .patch (* args , ** kwargs )
34
+ mocked = p .start ()
35
+ self ._patches .append (p )
36
+ return mocked
37
+
38
+
30
39
@pytest .yield_fixture
31
40
def mock ():
32
41
result = MockFixture ()
@@ -35,7 +44,6 @@ def mock():
35
44
36
45
37
46
class UnixFS (object ):
38
-
39
47
@classmethod
40
48
def rm (cls , filename ):
41
49
os .remove (filename )
@@ -45,12 +53,30 @@ def ls(cls, path):
45
53
return os .listdir (path )
46
54
47
55
48
- def test_fixture (mock , tmpdir ):
49
- mock .patch .object (os , 'remove' )
50
- (tmpdir / 'foo.txt' ).ensure ()
51
- UnixFS .rm (tmpdir / 'foo.txt' )
52
- os .remove .assert_called_once_with (tmpdir / 'foo.txt' )
53
- assert os .path .isfile (str (tmpdir / 'foo.txt' ))
54
- mock .stopall ()
55
- UnixFS .rm (str (tmpdir / 'foo.txt' ))
56
- assert not os .path .isfile (str (tmpdir / 'foo.txt' ))
56
+ @pytest .fixture
57
+ def check_unix_fs_mocked (tmpdir , mock ):
58
+ def check (mocked_rm ):
59
+ (tmpdir / 'foo.txt' ).ensure ()
60
+ UnixFS .rm (tmpdir / 'foo.txt' )
61
+ os .remove .assert_called_once_with (tmpdir / 'foo.txt' )
62
+ mocked_rm .assert_called_once_with (tmpdir / 'foo.txt' )
63
+ assert os .path .isfile (str (tmpdir / 'foo.txt' ))
64
+ mock .stopall ()
65
+ UnixFS .rm (str (tmpdir / 'foo.txt' ))
66
+ assert not os .path .isfile (str (tmpdir / 'foo.txt' ))
67
+
68
+ return check
69
+
70
+
71
+ def mock_using_patch_object (mock ):
72
+ return mock .patch .object (os , 'remove' )
73
+
74
+ def mock_using_patch (mock ):
75
+ return mock .patch ('os.remove' )
76
+
77
+ @pytest .mark .parametrize ('mock_fs' , [mock_using_patch_object , mock_using_patch ],
78
+ )
79
+ def test_fixture (mock_fs , mock , check_unix_fs_mocked ):
80
+ mocked_rm = mock_fs (mock )
81
+ #mocked_rm = mock.patch(os, 'remove')
82
+ check_unix_fs_mocked (mocked_rm )
0 commit comments