Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
context.memoize("strict_mock", lambda self: StrictMock())
def before(self):
target = StrictMock(template=sample_module.Target)
self.original_callable = None
self.real_target = target
self.target_arg = target
def __setattr__(self, name, value):
if self._is_magic_method(name):
# ...check whether we're allowed to mock...
if name in self._UNSETTABLE_MAGICS or (
name in StrictMock.__dict__ and name not in self._SETTABLE_MAGICS
):
raise UnsupportedMagic(self, name)
# ...or if it is something unsupported.
if name not in self._SETTABLE_MAGICS:
raise NotImplementedError(
f"StrictMock does not implement support for {name}"
)
mock_value = self._validate_and_wrap_mock_value(name, value)
setattr(type(self), name, mock_value)
def allows_setting_with_mock_with_invalid_type_template(
self,
):
self.strict_mock.non_callable = unittest.mock.Mock(
spec=int
)
self.strict_mock.non_callable = StrictMock(template=int)
def allows_setting_valid_type_with_templated_mock(self):
self.strict_mock.non_callable = unittest.mock.Mock(spec=str)
self.strict_mock.non_callable = StrictMock(template=str)
context.memoize("strict_mock", lambda self: StrictMock(template=Template))
context.memoize("key", lambda self: 1)
def before(self):
self.target = StrictMock(
template=sample_module.Target,
runtime_attrs=["dynamic_instance_method"],
)
self.original_callable = None
self.real_target = self.target
self.target_arg = self.target
def other_instances_are_not_mocked(self):
mock_callable(self.target_arg, self.callable_arg).to_return_value(
"mocked value"
)
self.assertEqual(
self.callable_target(*self.call_args, **self.call_kwargs),
"mocked value",
)
other_strict_mock = StrictMock(
template=sample_module.Target, runtime_attrs=runtime_attrs
)
mock_callable(other_strict_mock, self.callable_arg).to_return_value(
"other mocked value"
)
self.assertEqual(
getattr(other_strict_mock, self.callable_arg)(
*self.call_args, **self.call_kwargs
),
"other mocked value",
)
def get_target_mock(self):
return StrictMock(template=self.get_target_class())
async def before(self):
self.original_callable = None
self.real_target = StrictMock(template=sample_module.Target)
self.target_arg = self.real_target
self.call_args = ("1", "2")
self.call_kwargs = {"kwarg1": "1", "kwarg2": "2"}