Skip to content

Commit

Permalink
Merge pull request #70 from keis/mock-description
Browse files Browse the repository at this point in the history
Don't use describe_to of mock objects
  • Loading branch information
offbyone authored Oct 29, 2018
2 parents c04a19e + 4096982 commit 20a0d72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/hamcrest/core/base_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from hamcrest.core.description import Description
from hamcrest.core.selfdescribingvalue import SelfDescribingValue
from hamcrest.core.helpers.hasmethod import hasmethod
from hamcrest.core.helpers.ismock import ismock

class BaseDescription(Description):
"""Base class for all :py:class:`~hamcrest.core.description.Description`
Expand All @@ -21,7 +22,7 @@ def append_text(self, text):
return self

def append_description_of(self, value):
if hasmethod(value, 'describe_to'):
if not ismock(value) and hasmethod(value, 'describe_to'):
value.describe_to(self)
elif six.PY3 and isinstance(value, six.text_type):
self.append(repr(value))
Expand Down
15 changes: 15 additions & 0 deletions src/hamcrest/core/helpers/ismock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
MOCKTYPES = ()
try:
from mock import Mock
MOCKTYPES += (Mock,)
except ImportError:
pass
try:
from unittest.mock import Mock
MOCKTYPES += (Mock,)
except ImportError:
pass


def ismock(obj):
return isinstance(obj, MOCKTYPES)
8 changes: 8 additions & 0 deletions tests/hamcrest_unit_test/base_description_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from hamcrest.core.base_description import BaseDescription
from hamcrest.core.selfdescribing import SelfDescribing
from hamcrest.core.helpers.ismock import MOCKTYPES

__author__ = "Chris Rose"
__copyright__ = "Copyright 2015 hamcrest.org"
Expand Down Expand Up @@ -54,3 +55,10 @@ def test_append_description_types(desc, described, appended):
def test_string_in_python_syntax(desc, char, rep):
desc.append_string_in_python_syntax(char)
assert ''.join(desc.appended) == "'{0}'".format(rep)


@pytest.mark.parametrize("mock", MOCKTYPES)
def test_describe_mock(desc, mock):
m = mock()
desc.append_description_of(m)
assert ''.join(desc.appended) == str(m)

0 comments on commit 20a0d72

Please sign in to comment.