You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: replace unittest.mock usage with custom spy (#3)
MagicMock behavior in creation and usage didn't end up fitting cleanly into the Decoy API,
especially with asynchronous fakes and fakes that involve several layers of classes. This commit
replaces MagicMock with a very similar Spy class, that basically takes exactly what Decoy needs from
unittest.mock.
Copy file name to clipboardExpand all lines: README.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ def decoy() -> Decoy:
53
53
return Decoy()
54
54
```
55
55
56
-
Why is this important? The `Decoy` container tracks every test double that is created during a test so that you can define assertions using fully-typed rehearsals of your test double. It's important to wipe this slate clean for every test so you don't leak memory or have any state preservation between tests.
56
+
Why is this important? The `Decoy` container tracks every fake that is created during a test so that you can define assertions using fully-typed rehearsals of your test double. It's important to wipe this slate clean for every test so you don't leak memory or have any state preservation between tests.
57
57
58
58
[pytest]: https://docs.pytest.org/
59
59
@@ -115,16 +115,16 @@ from decoy import Decoy, verify
115
115
from .logger import Logger
116
116
117
117
deflog_warning(msg: str, logger: Logger) -> None:
118
-
logger.warn(msg)
118
+
logger.warn(msg)
119
119
120
120
deftest_log_warning(decoy: Decoy):
121
-
logger = decoy.create_decoy(spec=Logger)
121
+
logger = decoy.create_decoy(spec=Logger)
122
122
123
-
# call code under test
124
-
some_result = log_warning("oh no!", logger)
123
+
# call code under test
124
+
some_result = log_warning("oh no!", logger)
125
125
126
-
# verify double called correctly
127
-
decoy.verify(logger.warn("oh no!"))
126
+
# verify double called correctly
127
+
decoy.verify(logger.warn("oh no!"))
128
128
```
129
129
130
130
### Matchers
@@ -141,19 +141,19 @@ from decoy import Decoy, matchers
141
141
from .logger import Logger
142
142
143
143
deflog_warning(msg: str, logger: Logger) -> None:
144
-
logger.warn(msg)
144
+
logger.warn(msg)
145
145
146
146
deftest_log_warning(decoy: Decoy):
147
-
logger = decoy.create_decoy(spec=Logger)
148
-
149
-
# call code under test
150
-
some_result = log_warning(
151
-
"Oh no, something horrible went wrong with request ID abc123efg456",
152
-
logger=logger
153
-
)
154
-
155
-
# verify double called correctly
156
-
decoy.verify(
157
-
mock_logger.warn(matchers.StringMatching("something went wrong"))
158
-
)
147
+
logger = decoy.create_decoy(spec=Logger)
148
+
149
+
# call code under test
150
+
some_result = log_warning(
151
+
"Oh no, something horrible went wrong with request ID abc123efg456",
152
+
logger=logger
153
+
)
154
+
155
+
# verify double called correctly
156
+
decoy.verify(
157
+
mock_logger.warn(matchers.StringMatching("something went wrong"))
0 commit comments