Skip to content

Commit a84922b

Browse files
committed
Update test utils
1 parent 23dd0ef commit a84922b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

tests/utils.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
# from unittest import TestCase
2-
3-
41
def call(*args, **kwargs):
52
return (tuple(args), dict(kwargs))
63

74

85
class Mock:
96
"""A mock callable object that stores its calls."""
107

11-
def __init__(self):
8+
def __init__(self, return_value=None, side_effect=None):
9+
self.return_value = return_value
10+
self.side_effect = side_effect
1211
self._calls = []
1312

1413
def __call__(self, *args, **kwargs):
15-
self._calls.append(call(*args, **kwargs))
14+
"""Call the mock and store the call details."""
15+
self._calls.append(call(*args[1:], **kwargs)) # Skip the first argument (self)
16+
if self.side_effect:
17+
raise self.side_effect
18+
return self.return_value
1619

1720
def assert_called(self):
1821
"""Assert that the mock was called at least once."""
@@ -24,17 +27,22 @@ def assert_not_called(self):
2427

2528
def assert_called_with(self, *args, **kwargs):
2629
"""Assert that the mock was last called with the given arguments."""
27-
# First call should be self, so we prepend it
28-
expected_args = [self] + list(args)
29-
expectation = call(*expected_args, **kwargs)
30+
# Fail if no calls were made
31+
self.assert_called()
3032

3133
# Try to have a useful output for assertion failures
34+
expectation = call(*args, **kwargs)
3235
assert self._calls[-1] == expectation, "Expected call with {}, got {}".format(
3336
expectation, self._calls[-1]
3437
)
3538

3639
def assert_has_calls(self, calls):
3740
"""Assert that the mock has the expected calls with arguments."""
41+
assert calls, "Expected calls cannot be empty."
42+
43+
# Fail if no calls were made
44+
self.assert_called()
45+
3846
assert self._calls == calls, "Expected calls {}, got {}".format(
3947
calls, self._calls
4048
)

0 commit comments

Comments
 (0)