How to use the flexmock.Mock function in flexmock

To help you get started, we’ve selected a few flexmock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github has207 / flexmock / flexmock.py View on Github external
- name: string name of the attribute to replace

    Returns:
      - Expectation object which can be used to modify the expectations
        on the fake attribute
    """
    if name in UPDATED_ATTRS:
      raise FlexmockError('unable to replace flexmock methods')
    chained_methods = None
    obj = _getattr(self, '_object')
    if '.' in name:
      name, chained_methods = name.split('.', 1)
    name = _update_name_if_private(obj, name)
    _ensure_object_has_named_attribute(obj, name)
    if chained_methods:
      if (not isinstance(obj, Mock) and
          not hasattr(getattr(obj, name), '__call__')):
        return_value = _create_partial_mock(getattr(obj, name))
      else:
        return_value = Mock()
      self._create_expectation(obj, name, return_value)
      return return_value.should_receive(chained_methods)
    else:
      return self._create_expectation(obj, name)
github has207 / flexmock / flexmock.py View on Github external
obj = self._object
    if _hasattr(obj, name) and not hasattr(expectation, 'original'):
      expectation._update_original(name, obj)
      method_type = type(_getattr(expectation, 'original'))
      try:
        # TODO(herman): this is awful, fix this properly.
        # When a class/static method is mocked out on an *instance*
        # we need to fetch the type from the class
        method_type = type(_getattr(obj.__class__, name))
      except: pass
      if method_type in SPECIAL_METHODS:
        expectation.original_function = getattr(obj, name)
      expectation.method_type = method_type
    override = _setattr(obj, name, types.MethodType(method_instance, obj))
    expectation._local_override = override
    if (override and not _isclass(obj) and not isinstance(obj, Mock) and
        hasattr(obj.__class__, name)):
      self._update_class_for_magic_builtins(obj, name)
github has207 / flexmock / flexmock.py View on Github external
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
github has207 / flexmock / flexmock.py View on Github external
def _create_placeholder_mock_for_proper_teardown(self, obj, name, original):
    """Ensures that the given function is replaced on teardown."""
    mock = Mock()
    mock._object = obj
    expectation = Expectation(obj, name=name, original=original)
    FlexmockContainer.add_expectation(mock, expectation)
github has207 / flexmock / flexmock.py View on Github external
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
github has207 / flexmock / flexmock.py View on Github external
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
github has207 / flexmock / flexmock.py View on Github external
def _attach_flexmock_methods(mock, flexmock_class, obj):
  try:
    for attr in Mock.UPDATED_ATTRS:
      if hasattr(obj, attr):
        if (_get_code(getattr(obj, attr)) is not
            _get_code(getattr(flexmock_class, attr))):
          return False
    for attr in Mock.UPDATED_ATTRS:
      if hasattr(obj, '__dict__') and type(obj.__dict__) is dict:
        obj.__dict__[attr] = getattr(mock, attr)
      else:
        setattr(obj, attr, getattr(mock, attr))
  except TypeError:
    raise AttemptingToMockBuiltin
  return True