-
Notifications
You must be signed in to change notification settings - Fork 11
Open
Description
Currently the following will fail
from callee.general import Captor
from mock import Mock
mock = Mock()
mock(1)
captor = Captor()
mock.assert_called_with(captor)
assert 1 == captor.value # This is ok!
mock(2)
mock.assert_called_with(captor) # Fails with ValueError: a value has already been captured
This is somewhat inconsistent with how Mock::assert_called_with works. For example:
mock = Mock()
mock(1)
mock(2)
mock(3)
mock.assert_called_with(3) # This is ok!
There are multiple ways to deal with this.
- Allow
Captorto be created with anallow_multiple_capturesparameter and set the value to the last captured argument
captor = Captor(allow_multiple_captures=True) # False by default?
mock = Mock()
mock(1)
mock(2)
mock.assert_called_with(captor)
assert 2 == captor.value
- Allow
Captorto match multiple arguments and save them in a list
captor = Captor(matcher=lambda x: x < 2, allow_multiple_captures=True)
mock = Mock()
mock(1)
mock(2)
mock.assert_called_with(captor)
assert [1, 2] == captor.values
assert [Match(1, True), Match(2, False)] == captor.matches
- Create a
MultiCaptorCaptor that will collect all matches for a given argument
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels