Skip to content

Commit

Permalink
Issue #25195: Fix a regression in mock.MagicMock
Browse files Browse the repository at this point in the history
_Call is a subclass of tuple (changeset 3603bae63c13 only works
for classes) so we need to implement __ne__ ourselves.

Patch by Andrew Plummer.
  • Loading branch information
berkerpeksag committed Mar 27, 2016
1 parent fa0f62d commit ce91387
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Lib/unittest/mock.py
Expand Up @@ -2029,6 +2029,9 @@ def __eq__(self, other):
return (other_args, other_kwargs) == (self_args, self_kwargs)


__ne__ = object.__ne__


def __call__(self, *args, **kwargs):
if self.name is None:
return _Call(('', args, kwargs), name='()')
Expand Down
17 changes: 17 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Expand Up @@ -304,6 +304,17 @@ def test_call_args_comparison(self):
# an exception. See issue 24857.
self.assertFalse(mock.call_args == "a long sequence")


def test_calls_equal_with_any(self):
call1 = mock.call(mock.MagicMock())
call2 = mock.call(mock.ANY)

# Check that equality and non-equality is consistent even when
# comparing with mock.ANY
self.assertTrue(call1 == call2)
self.assertFalse(call1 != call2)


def test_assert_called_with(self):
mock = Mock()
mock()
Expand All @@ -319,6 +330,12 @@ def test_assert_called_with(self):
mock.assert_called_with(1, 2, 3, a='fish', b='nothing')


def test_assert_called_with_any(self):
m = MagicMock()
m(MagicMock())
m.assert_called_with(mock.ANY)


def test_assert_called_with_function_spec(self):
def f(a, b, c, d=None):
pass
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Expand Up @@ -94,6 +94,10 @@ Core and Builtins
Library
-------

- Issue #25195: Fix a regression in mock.MagicMock. _Call is a subclass of
tuple (changeset 3603bae63c13 only works for classes) so we need to
implement __ne__ ourselves. Patch by Andrew Plummer.

- Issue #26644: Raise ValueError rather than SystemError when a negative
length is passed to SSLSocket.recv() or read().

Expand Down

0 comments on commit ce91387

Please sign in to comment.