Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _create_partial_mock(obj_or_class, **kwargs):
matches = [x for x in FlexmockContainer.flexmock_objects
if x._object is obj_or_class]
if matches:
mock = matches[0]
else:
mock = Mock()
mock._object = obj_or_class
for name, return_value in kwargs.items():
if hasattr(return_value, '__call__'):
mock.should_receive(name).replace_with(return_value)
else:
mock.should_receive(name).and_return(return_value)
if not matches:
FlexmockContainer.add_expectation(mock, Expectation(obj_or_class))
if (_attach_flexmock_methods(mock, Mock, obj_or_class) and
not _isclass(mock._object)):
mock = mock._object
return mock
return_values.append(return_value)
else:
return_value = ReturnValue()
if return_value.raises:
if _isclass(return_value.raises):
raise return_value.raises(
*return_value.value['kargs'], **return_value.value['kwargs'])
else:
raise return_value.raises
else:
return return_value.value
else:
# make sure to clean up expectations to ensure none of them
# interfere with the runner's error reporing mechanism
# e.g. open()
for _, expectations in FlexmockContainer.flexmock_objects.items():
for expectation in expectations:
_getattr(expectation, 'reset')()
raise MethodSignatureError(_format_args(name, arguments))
def _create_partial_mock(obj_or_class, **kwargs):
matches = [x for x in FlexmockContainer.flexmock_objects
if x._object is obj_or_class]
if matches:
mock = matches[0]
else:
mock = Mock()
mock._object = obj_or_class
for method, return_value in kwargs.items():
mock.should_receive(method).and_return(return_value)
if not matches:
FlexmockContainer.add_expectation(mock, Expectation(obj_or_class))
if (_attach_flexmock_methods(mock, Mock, obj_or_class) and
not inspect.isclass(mock._object)):
mock = mock._object
return mock
obj = self._object
if not _isclass(obj):
obj = obj.__class__
expectation._callable = False
original = getattr(obj, name)
@property
def updated(self):
if (hasattr(self, '__dict__') and type(self.__dict__) is dict and
name in self.__dict__):
return self.__dict__[name]
else:
return getattr(self, new_name)
setattr(obj, name, updated)
if not hasattr(obj, new_name):
# don't try to double update
FlexmockContainer.add_teardown_property(obj, new_name)
setattr(obj, new_name, original)
self._create_placeholder_mock_for_proper_teardown(obj, name, original)
def ordered(self):
"""Makes the expectation respect the order of should_receive statements.
An exception will be raised if methods are called out of order, determined
by order of should_receive calls in the test.
Returns:
- self, i.e. can be chained with other Expectation methods
"""
if not self._callable:
self.__raise(FlexmockError, "can't use ordered() with attribute stubs")
self._ordered = True
FlexmockContainer.ordered.append(self)
return self