Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Captor to capture multiple arguments #11

Open
NazarioJL opened this issue Apr 9, 2019 · 0 comments
Open

Allow Captor to capture multiple arguments #11

NazarioJL opened this issue Apr 9, 2019 · 0 comments

Comments

@NazarioJL
Copy link

NazarioJL commented Apr 9, 2019

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 Captor to be created with an allow_multiple_captures parameter 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 Captor to 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 MultiCaptor Captor that will collect all matches for a given argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant