-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(when): warn if stub is called with arguments that do not match r…
…ehearsals (#16) This commit corrects the bug that was present in the previously reverted implementation of this feature. Closes #14
- Loading branch information
Showing
7 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Warnings produced by Decoy.""" | ||
from os import linesep | ||
from typing import Any, Sequence | ||
|
||
from .spy import SpyCall | ||
from .stub import Stub | ||
|
||
|
||
class MissingStubWarning(UserWarning): | ||
"""A warning raised when a configured stub is called with different arguments.""" | ||
|
||
def __init__(self, call: SpyCall, stubs: Sequence[Stub[Any]]) -> None: | ||
"""Initialize the warning message with the actual and expected calls.""" | ||
stubs_len = len(stubs) | ||
stubs_plural = stubs_len != 1 | ||
stubs_printout = linesep.join( | ||
[f"{n + 1}.\t{str(stubs[n]._rehearsal)}" for n in range(stubs_len)] | ||
) | ||
|
||
message = linesep.join( | ||
[ | ||
"Stub was called but no matching rehearsal found.", | ||
f"Found {stubs_len} rehearsal{'s' if stubs_plural else ''}:", | ||
stubs_printout, | ||
"Actual call:", | ||
f"\t{str(call)}", | ||
] | ||
) | ||
|
||
super().__init__(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
::: decoy.stub.Stub | ||
|
||
::: decoy.matchers | ||
|
||
::: decoy.warnings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Tests for warning messages.""" | ||
from os import linesep | ||
from typing import Any, List | ||
|
||
from decoy.spy import SpyCall | ||
from decoy.stub import Stub | ||
from decoy.warnings import MissingStubWarning | ||
|
||
|
||
def test_no_stubbing_found_warning() -> None: | ||
"""It should print a helpful error message if a call misses a stub.""" | ||
call = SpyCall(spy_id=123, spy_name="spy", args=(1, 2), kwargs={"foo": "bar"}) | ||
stub: Stub[Any] = Stub( | ||
rehearsal=SpyCall( | ||
spy_id=123, | ||
spy_name="spy", | ||
args=(3, 4), | ||
kwargs={"baz": "qux"}, | ||
) | ||
) | ||
|
||
result = MissingStubWarning(call=call, stubs=[stub]) | ||
|
||
assert str(result) == ( | ||
f"Stub was called but no matching rehearsal found.{linesep}" | ||
f"Found 1 rehearsal:{linesep}" | ||
f"1.\tspy(3, 4, baz='qux'){linesep}" | ||
f"Actual call:{linesep}" | ||
"\tspy(1, 2, foo='bar')" | ||
) | ||
|
||
|
||
def test_no_stubbing_found_warning_plural() -> None: | ||
"""It should print a helpful message if a call misses multiple stubs.""" | ||
call = SpyCall(spy_id=123, spy_name="spy", args=(1, 2), kwargs={"foo": "bar"}) | ||
stubs: List[Stub[Any]] = [ | ||
Stub( | ||
rehearsal=SpyCall( | ||
spy_id=123, | ||
spy_name="spy", | ||
args=(3, 4), | ||
kwargs={"baz": "qux"}, | ||
) | ||
), | ||
Stub( | ||
rehearsal=SpyCall( | ||
spy_id=123, | ||
spy_name="spy", | ||
args=(5, 6), | ||
kwargs={"fizz": "buzz"}, | ||
) | ||
), | ||
] | ||
|
||
result = MissingStubWarning(call=call, stubs=stubs) | ||
|
||
assert str(result) == ( | ||
f"Stub was called but no matching rehearsal found.{linesep}" | ||
f"Found 2 rehearsals:{linesep}" | ||
f"1.\tspy(3, 4, baz='qux'){linesep}" | ||
f"2.\tspy(5, 6, fizz='buzz'){linesep}" | ||
f"Actual call:{linesep}" | ||
"\tspy(1, 2, foo='bar')" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters