Skip to content

Allow Captor to capture multiple arguments #11

@NazarioJL

Description

@NazarioJL

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions