How to use the flexmock.ReturnValue 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
else:
      value = values

    if not self._callable:
      _setattr(self._mock, self.name, value)
      return self

    return_values = _getattr(self, 'return_values')
    if not _getattr(self, '_one_by_one'):
      value = ReturnValue(value)
      return_values.append(value)
    else:
      try:
        return_values.extend([ReturnValue(v) for v in value])
      except TypeError:
        return_values.append(ReturnValue(value))
    return self
github has207 / flexmock / flexmock.py View on Github external
def __init__(self, mock, name=None, return_value=None, original_method=None):
    self.method = name
    self.modifier = ''
    self.original_method = original_method
    self.args = None
    value = ReturnValue(return_value)
    self.return_values = []
    self._replace_with = None
    if return_value is not None:
      self.return_values.append(value)
    self.yield_values = []
    self.times_called = 0
    self.expected_calls = None
    self._mock = mock
    self._pass_thru = False
    self._ordered = False
    self._one_by_one = False
github has207 / flexmock / flexmock.py View on Github external
def and_raise(self, exception, *kargs, **kwargs):
    """Specifies the exception to be raised when this expectation is met.

    Args:
      - exception: class or instance of the exception
      - kargs: optional keyword arguments to pass to the exception
      - kwargs: optional named arguments to pass to the exception

    Returns:
      - self, i.e. can be chained with other Expectation methods
    """
    args = {'kargs': kargs, 'kwargs': kwargs}
    self.return_values.append(ReturnValue(raises=exception, value=args))
    return self
github has207 / flexmock / flexmock.py View on Github external
Args:
      - values: optional list of return values, defaults to None if not given

    Returns:
      - self, i.e. can be chained with other Expectation methods
    """
    if len(values) == 1:
      value = values[0]
    else:
      value = values
    if not self._one_by_one:
      value = ReturnValue(value)
      self.return_values.append(value)
    else:
      try:
        self.return_values.extend([ReturnValue(v) for v in value])
      except TypeError:
        self.return_values.append(ReturnValue(value))
    return self
github has207 / flexmock / flexmock.py View on Github external
def __init__(self, mock, name=None, return_value=None, original=None):
    self.name = name
    self.modifier = EXACTLY
    if original is not None:
      self.original = original
    self.args = None
    self.method_type = types.MethodType
    self.argspec = None
    value = ReturnValue(return_value)
    self.return_values = return_values = []
    self._replace_with = None
    if return_value is not None:
      return_values.append(value)
    self.times_called = 0
    self.expected_calls = {
        EXACTLY: None,
        AT_LEAST: None,
        AT_MOST: None}
    self.runnable = lambda: True
    self._mock = mock
    self._pass_thru = False
    self._ordered = False
    self._one_by_one = False
    self._verified = False
    self._callable = True
github has207 / flexmock / flexmock.py View on Github external
(name, expectation._get_runnable()))
        expectation.times_called += 1
        expectation.verify(final=False)
        _pass_thru = _getattr(expectation, '_pass_thru')
        _replace_with = _getattr(expectation, '_replace_with')
        if _pass_thru:
          return pass_thru(expectation, runtime_self, *kargs, **kwargs)
        elif _replace_with:
          return _replace_with(*kargs, **kwargs)
        return_values = _getattr(expectation, 'return_values')
        if return_values:
          return_value = return_values[0]
          del return_values[0]
          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))
github has207 / flexmock / flexmock.py View on Github external
def and_raise(self, exception, *kargs, **kwargs):
    """Specifies the exception to be raised when this expectation is met.

    Args:
      - exception: class or instance of the exception
      - kargs: optional keyword arguments to pass to the exception
      - kwargs: optional named arguments to pass to the exception

    Returns:
      - self, i.e. can be chained with other Expectation methods
    """
    if not self._callable:
      self.__raise(FlexmockError, "can't use and_raise() with attribute stubs")
    args = {'kargs': kargs, 'kwargs': kwargs}
    return_values = _getattr(self, 'return_values')
    return_values.append(ReturnValue(raises=exception, value=args))
    return self
github has207 / flexmock / flexmock.py View on Github external
elif len(values) == 1:
      value = values[0]
    else:
      value = values

    if not self._callable:
      _setattr(self._mock, self.name, value)
      return self

    return_values = _getattr(self, 'return_values')
    if not _getattr(self, '_one_by_one'):
      value = ReturnValue(value)
      return_values.append(value)
    else:
      try:
        return_values.extend([ReturnValue(v) for v in value])
      except TypeError:
        return_values.append(ReturnValue(value))
    return self
github has207 / flexmock / flexmock.py View on Github external
Each value in the list is returned on successive invocations of the method.

    This is a property method so must be called without parentheses.

    Returns:
      - self, i.e. can be chained with other Expectation methods
    """
    if not self._one_by_one:
      self._one_by_one = True
      saved_values = self.return_values[:]
      self.return_values = []
      for value in saved_values:
        try:
          for val in value.value:
            self.return_values.append(ReturnValue(val))
        except TypeError:
          self.return_values.append(value)
    return self
github has207 / flexmock / flexmock.py View on Github external
Returns:
      - self, i.e. can be chained with other Expectation methods
    """
    if len(values) == 1:
      value = values[0]
    else:
      value = values
    if not self._one_by_one:
      value = ReturnValue(value)
      self.return_values.append(value)
    else:
      try:
        self.return_values.extend([ReturnValue(v) for v in value])
      except TypeError:
        self.return_values.append(ReturnValue(value))
    return self