Skip to content

Commit

Permalink
[strict_mock] Add support mocking other mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
david-caro committed Mar 30, 2020
1 parent a11c344 commit 799086d
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions testslide/strict_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,41 @@
import functools
import inspect
import os.path
from unittest.mock import Mock, PropertyMock, AsyncMock, MagicMock, NonCallableMagicMock, _must_skip

from unittest.mock import _must_skip
from .lib import _validate_function_signature


def _get_spec_from_strict_mock(mock_obj: StrictMock) -> Optional[Type]:
if "__template" in mock_obj.__dict__:
return mock_obj.__template

return None


def _get_spec_from_mock(mock_obj: Mock) -> Optional[Type]:
if "_spec_class" in mock_obj.__dict__:
return mock_obj._spec_class

return None


MOCK_TO_TEMPLATE_EXTRACTORS = {
StrictMock: _get_spec_from_strict_mock,
Mock: _get_spec_from_mock,
PropertyMock: _get_spec_from_mock,
AsyncMock: _get_spec_from_mock,
MagicMock: _get_spec_from_mock,
NonCallableMagicMock: _get_spec_from_mock,
}


def _wrap_signature_and_type_validation(value, template, attr_name):
if isinstance(template, StrictMock):
if "_template" in template.__dict__:
template = template._template
else:
return value
template = MOCK_TO_TEMPLATE_EXTRACTORS.get(
type(self.__template), lambda x: None
)()
if not template
return value

# This covers runtime attributes
if not hasattr(template, attr_name):
Expand Down

0 comments on commit 799086d

Please sign in to comment.