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
This is a pretty verbose way of writing a test, so in general, you may want to approach using `matchers.Captor` as a form of potential code smell / test pain. There are often better ways to structure your code for these sorts of interactions that don't involve private functions.
81
81
82
82
For further reading on when (or rather, when not) to use argument captors, check out [testdouble's documentation on its argument captor matcher](https://github.com/testdouble/testdouble.js/blob/main/docs/6-verifying-invocations.md#tdmatcherscaptor).
83
+
84
+
## Writing custom matchers
85
+
86
+
You can write your own matcher class and use it wherever you would use a built-in matcher. All you need to do is define a class with an `__eq__` method:
87
+
88
+
```python
89
+
classIs42:
90
+
def__eq__(self, other: object) -> bool:
91
+
return other ==42
92
+
93
+
check_answer = decoy.mock(name="check_answer")
94
+
95
+
decoy.when(
96
+
check_answer(Is42())
97
+
).then_return("huzzah!")
98
+
99
+
assert check_answer(42) =="huzzah!"
100
+
assert check_answer(43) isNone
101
+
```
102
+
103
+
This is especially useful if the value objects you are using as arguments are difficult to compare and out of your control. For example, Pandas [DataFrame][] objects do not return a `bool` from `__eq__`, which makes it difficult to compare calls.
104
+
105
+
We can define a `MatchesDataFrame` class to work around this:
0 commit comments