How can i check call arguments if they will change with unittest.mock

前端 未结 1 449
情话喂你
情话喂你 2020-12-22 02:30

One of my classes accumulates values in a list, uses the list as an argument to a method on another object and deletes some of the values in this list. Something like

<
相关标签:
1条回答
  • 2020-12-22 03:03

    There is a chapter "Coping with mutable arguments" in the documentation, which suggests several solutions to your problem.

    I'd go with this one:

    >>> from copy import deepcopy
    >>> class CopyingMock(MagicMock):
    ...     def __call__(self, *args, **kwargs):
    ...         args = deepcopy(args)
    ...         kwargs = deepcopy(kwargs)
    ...         return super(CopyingMock, self).__call__(*args, **kwargs)
    ...
    >>> c = CopyingMock(return_value=None)
    >>> arg = set()
    >>> c(arg)
    >>> arg.add(1)
    >>> c.assert_called_with(set())
    >>> c.assert_called_with(arg)
    Traceback (most recent call last):
        ...
    AssertionError: Expected call: mock(set([1]))
    Actual call: mock(set([]))
    >>> c.foo
    <CopyingMock name='mock.foo' id='...'>
    
    0 讨论(0)
提交回复
热议问题