8
8
9
9
def test_call_function_then_verify (decoy : Decoy ) -> None :
10
10
"""It should be able to verify a past function call."""
11
- stub = decoy .create_decoy_func (spec = some_func )
11
+ spy = decoy .create_decoy_func (spec = some_func )
12
12
13
- stub ("hello" )
14
- stub ("goodbye" )
13
+ spy ("hello" )
14
+ spy ("goodbye" )
15
15
16
- decoy .verify (stub ("hello" ))
17
- decoy .verify (stub ("goodbye" ))
16
+ decoy .verify (spy ("hello" ))
17
+ decoy .verify (spy ("goodbye" ))
18
18
19
19
with pytest .raises (AssertionError ) as error_info :
20
- decoy .verify (stub ("fizzbuzz" ))
20
+ decoy .verify (spy ("fizzbuzz" ))
21
21
22
22
assert str (error_info .value ) == (
23
23
f"Expected call:{ linesep } "
@@ -30,21 +30,21 @@ def test_call_function_then_verify(decoy: Decoy) -> None:
30
30
31
31
def test_call_method_then_verify (decoy : Decoy ) -> None :
32
32
"""It should be able to verify a past method call."""
33
- stub = decoy .create_decoy (spec = SomeClass )
33
+ spy = decoy .create_decoy (spec = SomeClass )
34
34
35
- stub .foo ("hello" )
36
- stub .foo ("goodbye" )
37
- stub .bar (0 , 1.0 , "2" )
38
- stub .bar (3 , 4.0 , "5" )
35
+ spy .foo ("hello" )
36
+ spy .foo ("goodbye" )
37
+ spy .bar (0 , 1.0 , "2" )
38
+ spy .bar (3 , 4.0 , "5" )
39
39
40
- decoy .verify (stub .foo ("hello" ))
41
- decoy .verify (stub .foo ("goodbye" ))
40
+ decoy .verify (spy .foo ("hello" ))
41
+ decoy .verify (spy .foo ("goodbye" ))
42
42
43
- decoy .verify (stub .bar (0 , 1.0 , "2" ))
44
- decoy .verify (stub .bar (3 , 4.0 , "5" ))
43
+ decoy .verify (spy .bar (0 , 1.0 , "2" ))
44
+ decoy .verify (spy .bar (3 , 4.0 , "5" ))
45
45
46
46
with pytest .raises (AssertionError ) as error_info :
47
- decoy .verify (stub .foo ("fizzbuzz" ))
47
+ decoy .verify (spy .foo ("fizzbuzz" ))
48
48
49
49
assert str (error_info .value ) == (
50
50
f"Expected call:{ linesep } "
@@ -55,7 +55,7 @@ def test_call_method_then_verify(decoy: Decoy) -> None:
55
55
)
56
56
57
57
with pytest .raises (AssertionError ) as error_info :
58
- decoy .verify (stub .bar (6 , 7.0 , "8" ))
58
+ decoy .verify (spy .bar (6 , 7.0 , "8" ))
59
59
60
60
assert str (error_info .value ) == (
61
61
f"Expected call:{ linesep } "
@@ -68,14 +68,14 @@ def test_call_method_then_verify(decoy: Decoy) -> None:
68
68
69
69
def test_verify_with_matcher (decoy : Decoy ) -> None :
70
70
"""It should still work with matchers as arguments."""
71
- stub = decoy .create_decoy_func (spec = some_func )
71
+ spy = decoy .create_decoy_func (spec = some_func )
72
72
73
- stub ("hello" )
73
+ spy ("hello" )
74
74
75
- decoy .verify (stub (matchers .StringMatching ("ell" )))
75
+ decoy .verify (spy (matchers .StringMatching ("ell" )))
76
76
77
77
with pytest .raises (AssertionError ) as error_info :
78
- decoy .verify (stub (matchers .StringMatching ("^ell" )))
78
+ decoy .verify (spy (matchers .StringMatching ("^ell" )))
79
79
80
80
assert str (error_info .value ) == (
81
81
f"Expected call:{ linesep } "
@@ -87,48 +87,48 @@ def test_verify_with_matcher(decoy: Decoy) -> None:
87
87
88
88
def test_call_nested_method_then_verify (decoy : Decoy ) -> None :
89
89
"""It should be able to verify a past nested method call."""
90
- stub = decoy .create_decoy (spec = SomeNestedClass )
90
+ spy = decoy .create_decoy (spec = SomeNestedClass )
91
91
92
- stub .child .foo ("hello" )
93
- stub .child .bar (0 , 1.0 , "2" )
92
+ spy .child .foo ("hello" )
93
+ spy .child .bar (0 , 1.0 , "2" )
94
94
95
- decoy .verify (stub .child .foo ("hello" ))
96
- decoy .verify (stub .child .bar (0 , 1.0 , "2" ))
95
+ decoy .verify (spy .child .foo ("hello" ))
96
+ decoy .verify (spy .child .bar (0 , 1.0 , "2" ))
97
97
98
98
with pytest .raises (AssertionError ):
99
- decoy .verify (stub .foo ("fizzbuzz" ))
99
+ decoy .verify (spy .foo ("fizzbuzz" ))
100
100
101
101
102
102
def test_call_no_return_method_then_verify (decoy : Decoy ) -> None :
103
103
"""It should be able to verify a past void method call."""
104
- stub = decoy .create_decoy (spec = SomeClass )
104
+ spy = decoy .create_decoy (spec = SomeClass )
105
105
106
- stub .do_the_thing (True )
106
+ spy .do_the_thing (True )
107
107
108
- decoy .verify (stub .do_the_thing (True ))
108
+ decoy .verify (spy .do_the_thing (True ))
109
109
110
110
with pytest .raises (AssertionError ):
111
- decoy .verify (stub .do_the_thing (False ))
111
+ decoy .verify (spy .do_the_thing (False ))
112
112
113
113
114
114
def test_verify_multiple_calls (decoy : Decoy ) -> None :
115
115
"""It should be able to verify multiple calls."""
116
- stub = decoy .create_decoy (spec = SomeClass )
117
- stub_func = decoy .create_decoy_func (spec = some_func )
116
+ spy = decoy .create_decoy (spec = SomeClass )
117
+ spy_func = decoy .create_decoy_func (spec = some_func )
118
118
119
- stub .do_the_thing (False )
120
- stub .do_the_thing (True )
121
- stub_func ("hello" )
119
+ spy .do_the_thing (False )
120
+ spy .do_the_thing (True )
121
+ spy_func ("hello" )
122
122
123
123
decoy .verify (
124
- stub .do_the_thing (True ),
125
- stub_func ("hello" ),
124
+ spy .do_the_thing (True ),
125
+ spy_func ("hello" ),
126
126
)
127
127
128
128
with pytest .raises (AssertionError ) as error_info :
129
129
decoy .verify (
130
- stub .do_the_thing (False ),
131
- stub_func ("goodbye" ),
130
+ spy .do_the_thing (False ),
131
+ spy_func ("goodbye" ),
132
132
)
133
133
134
134
assert str (error_info .value ) == (
@@ -143,8 +143,8 @@ def test_verify_multiple_calls(decoy: Decoy) -> None:
143
143
144
144
with pytest .raises (AssertionError ) as error_info :
145
145
decoy .verify (
146
- stub_func ("hello" ),
147
- stub .do_the_thing (True ),
146
+ spy_func ("hello" ),
147
+ spy .do_the_thing (True ),
148
148
)
149
149
150
150
assert str (error_info .value ) == (
@@ -159,9 +159,9 @@ def test_verify_multiple_calls(decoy: Decoy) -> None:
159
159
160
160
with pytest .raises (AssertionError ) as error_info :
161
161
decoy .verify (
162
- stub .do_the_thing (True ),
163
- stub .do_the_thing (True ),
164
- stub_func ("hello" ),
162
+ spy .do_the_thing (True ),
163
+ spy .do_the_thing (True ),
164
+ spy_func ("hello" ),
165
165
)
166
166
167
167
assert str (error_info .value ) == (
@@ -174,3 +174,56 @@ def test_verify_multiple_calls(decoy: Decoy) -> None:
174
174
f"2.\t SomeClass.do_the_thing(True){ linesep } "
175
175
"3.\t some_func('hello')"
176
176
)
177
+
178
+
179
+ def test_verify_call_count (decoy : Decoy ) -> None :
180
+ """It should be able to verify a specific call count."""
181
+ spy = decoy .create_decoy_func (spec = some_func )
182
+
183
+ spy ("hello" )
184
+ spy ("hello" )
185
+
186
+ decoy .verify (spy ("hello" ))
187
+ decoy .verify (spy ("hello" ), times = 2 )
188
+ decoy .verify (spy ("goodbye" ), times = 0 )
189
+
190
+ with pytest .raises (AssertionError ) as error_info :
191
+ decoy .verify (spy ("hello" ), times = 0 )
192
+
193
+ assert str (error_info .value ) == (
194
+ f"Expected 0 calls:{ linesep } "
195
+ f"1.\t some_func('hello'){ linesep } "
196
+ f"Found 2 calls:{ linesep } "
197
+ f"1.\t some_func('hello'){ linesep } "
198
+ "2.\t some_func('hello')"
199
+ )
200
+
201
+ with pytest .raises (AssertionError ) as error_info :
202
+ decoy .verify (spy ("hello" ), times = 1 )
203
+
204
+ assert str (error_info .value ) == (
205
+ f"Expected 1 call:{ linesep } "
206
+ f"1.\t some_func('hello'){ linesep } "
207
+ f"Found 2 calls:{ linesep } "
208
+ f"1.\t some_func('hello'){ linesep } "
209
+ "2.\t some_func('hello')"
210
+ )
211
+
212
+ with pytest .raises (AssertionError ) as error_info :
213
+ decoy .verify (spy ("hello" ), times = 3 )
214
+
215
+ assert str (error_info .value ) == (
216
+ f"Expected 3 calls:{ linesep } "
217
+ f"1.\t some_func('hello'){ linesep } "
218
+ f"Found 2 calls:{ linesep } "
219
+ f"1.\t some_func('hello'){ linesep } "
220
+ "2.\t some_func('hello')"
221
+ )
222
+
223
+
224
+ def test_verify_call_count_raises_multiple_rehearsals (decoy : Decoy ) -> None :
225
+ """It should not be able to verify call count if multiple rehearsals used."""
226
+ spy = decoy .create_decoy_func (spec = some_func )
227
+
228
+ with pytest .raises (ValueError , match = "multiple rehearsals" ):
229
+ decoy .verify (spy ("hello" ), spy ("goodbye" ), times = 1 )
0 commit comments