|
1 |
| -"""Tests for spy call verification.""" |
| 1 | +"""Tests for the WarningChecker API.""" |
2 | 2 | import pytest
|
3 |
| -from typing import Any, List, NamedTuple, Sequence |
| 3 | +from typing import List, NamedTuple, Sequence |
4 | 4 |
|
5 | 5 | from decoy import matchers
|
6 | 6 | from decoy.spy_calls import BaseSpyCall, SpyCall, WhenRehearsal, VerifyRehearsal
|
@@ -172,6 +172,34 @@ class WarningCheckerSpec(NamedTuple):
|
172 | 172 | ),
|
173 | 173 | ],
|
174 | 174 | ),
|
| 175 | + # it should not warn if a call misses a stubbing but is later verified |
| 176 | + WarningCheckerSpec( |
| 177 | + all_calls=[ |
| 178 | + WhenRehearsal(spy_id=1, spy_name="spy", args=(1,), kwargs={}), |
| 179 | + SpyCall(spy_id=1, spy_name="spy", args=(2,), kwargs={}), |
| 180 | + VerifyRehearsal(spy_id=1, spy_name="spy", args=(2,), kwargs={}), |
| 181 | + ], |
| 182 | + expected_warnings=[], |
| 183 | + ), |
| 184 | + # it should warn if a call misses a stubbing after it is verified |
| 185 | + WarningCheckerSpec( |
| 186 | + all_calls=[ |
| 187 | + SpyCall(spy_id=1, spy_name="spy", args=(2,), kwargs={}), |
| 188 | + VerifyRehearsal(spy_id=1, spy_name="spy", args=(2,), kwargs={}), |
| 189 | + WhenRehearsal(spy_id=1, spy_name="spy", args=(1,), kwargs={}), |
| 190 | + SpyCall(spy_id=1, spy_name="spy", args=(2,), kwargs={}), |
| 191 | + ], |
| 192 | + expected_warnings=[ |
| 193 | + MiscalledStubWarning( |
| 194 | + rehearsals=[ |
| 195 | + WhenRehearsal(spy_id=1, spy_name="spy", args=(1,), kwargs={}), |
| 196 | + ], |
| 197 | + calls=[ |
| 198 | + SpyCall(spy_id=1, spy_name="spy", args=(2,), kwargs={}), |
| 199 | + ], |
| 200 | + ), |
| 201 | + ], |
| 202 | + ), |
175 | 203 | # it should issue a redundant verify warning if a call has a when and a verify
|
176 | 204 | WarningCheckerSpec(
|
177 | 205 | all_calls=[
|
@@ -210,14 +238,16 @@ def test_verify_no_misscalled_stubs(
|
210 | 238 | subject = WarningChecker()
|
211 | 239 | subject.check(all_calls)
|
212 | 240 |
|
213 |
| - assert len(recwarn) == len(expected_warnings) |
214 |
| - for expected in expected_warnings: |
215 |
| - result: Any = recwarn.pop(DecoyWarning).message |
216 |
| - expected_attr = {} |
| 241 | + warning_matchers = [] |
| 242 | + |
| 243 | + for warning in expected_warnings: |
| 244 | + if isinstance(warning, MiscalledStubWarning): |
| 245 | + warning_attr = {"rehearsals": warning.rehearsals, "calls": warning.calls} |
| 246 | + elif isinstance(warning, RedundantVerifyWarning): |
| 247 | + warning_attr = {"rehearsal": warning.rehearsal} |
| 248 | + |
| 249 | + warning_matchers.append(matchers.IsA(type(warning), warning_attr)) |
217 | 250 |
|
218 |
| - if isinstance(expected, MiscalledStubWarning): |
219 |
| - expected_attr = {"rehearsals": expected.rehearsals, "calls": expected.calls} |
220 |
| - elif isinstance(expected, RedundantVerifyWarning): |
221 |
| - expected_attr = {"rehearsal": expected.rehearsal} |
| 251 | + actual_warnings = [record.message for record in recwarn] |
222 | 252 |
|
223 |
| - assert result == matchers.IsA(type(expected), expected_attr) |
| 253 | + assert actual_warnings == warning_matchers |
0 commit comments