Skip to content

Commit 03f890b

Browse files
committed
Allow multiple entrance
1 parent bd87396 commit 03f890b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

mockito/inorder.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ def __init__(self, *objects: object):
4949
f"{[str(d) for d in duplicates]}"
5050
)
5151
self._objects = objects
52-
for obj in objects:
52+
self._attach_all()
53+
self.ordered_invocations: Deque[RealInvocation] = deque()
54+
55+
def _attach_all(self):
56+
for obj in self._objects:
5357
if m := mock_registry.mock_for(obj):
5458
m.attach(self)
5559

56-
self.ordered_invocations: Deque[RealInvocation] = deque()
57-
5860
def update(self, invocation: RealInvocation) -> None:
5961
self.ordered_invocations.append(invocation)
6062

@@ -93,6 +95,7 @@ def verify(
9395
)
9496

9597
def __enter__(self):
98+
self._attach_all()
9699
return self
97100

98101
def __exit__(self, exc_type, exc_val, exc_tb):

mockito/mocking.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ def __init__(
7272
self._observers: list = []
7373

7474
def attach(self, observer) -> None:
75-
self._observers.append(observer)
75+
if observer not in self._observers:
76+
self._observers.append(observer)
7677

7778
def detach(self, observer) -> None:
78-
self._observers.remove(observer)
79+
try:
80+
self._observers.remove(observer)
81+
except ValueError:
82+
pass
7983

8084
def remember(self, invocation: invocation.RealInvocation) -> None:
8185
self.invocations.append(invocation)

tests/in_order_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ def test_do_not_record_after_detach():
161161
in_order.verify(cat).meow()
162162

163163

164+
def test_allow_double_entrance():
165+
cat = mock()
166+
in_order = InOrder(cat)
167+
with in_order:
168+
pass
169+
cat.meow()
170+
with in_order:
171+
cat.meow()
172+
in_order.verify(cat, times=1).meow()
173+
174+
164175
def test_in_order_verify_times_across_mocks():
165176
cat = mock()
166177
dog = mock()

0 commit comments

Comments
 (0)