Created
May 12, 2019 17:34
-
-
Save dishbreak/0f65fa775da493b20af9aee89e2ae45f to your computer and use it in GitHub Desktop.
Assert a Partial Match on a MagicMock Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import mock | |
class AnyStringEndingWith(object): | |
def __init__(self, suffix): | |
self.suffix = suffix | |
def __repr__(self): | |
return "<a string ending with '%s'>" % self.suffix | |
def __eq__(self, other): | |
return other.endswith(self.suffix) | |
@mock.patch("os.path") | |
def test_matcher(mock_path): | |
mock_path.isfile(os.path.join("not", "a", "real", "path")) | |
mock_path.isfile.assert_called_with(AnyStringEndingWith("path")) |
Nice! If anyone’s curious why that works, it’s because assert_called_with
(and family of such) converts the args, kwargs
of the call into a tuple
, then does:
expected = self._call_matcher((args, kwargs))
actual = self._call_matcher(self.call_args)
if expected != actual:
raise ...
Tuple equality == equality of each ordered element in the tuple. So, by overriding the __eq__
of any object that is passed in, one can control the conditions of match.
For another example, check out what the sentinel object mock.ANY
does:
class _ANY(object):
"A helper object that compares equal to everything."
def __eq__(self, other):
return True
def __ne__(self, other):
return False
def __repr__(self):
return '<ANY>'
ANY = _ANY()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from this answer on StackOverflow.