mockify.actions - Classes for recording side effects

Module containing action types.

Actions are used to tell the mock what it must do once called with given set of arguments. This can be returning a value, returning a generator, calling a function, raising exception etc.

class mockify.actions.Action

Bases: abc.ABC, mockify._utils.DictEqualityMixin

Abstract base class for actions.

This is common base class for all actions defined in this module. Custom actions should also inherit from this one.

New in version 0.6.

__call__(actual_call)

Action body.

It receives actual call object and returns action result based on that call object. This method may also raise exceptions if that is functionality of the action being implemented.

Parameters:actual_call – Instance of mockify.core.Call containing params of actual call being made
format_params(*args, **kwargs)

Used to calculate str() and repr() for this action.

This method should be overloaded in subclass as a no argument method, and then call super().format_params(…) with args and kwargs you want to be included in str() and repr() methods.

class mockify.actions.Return(value)

Bases: mockify.actions.Action

Forces mock to return value when called.

For example:

>>> from mockify.mock import Mock
>>> from mockify.actions import Return
>>> mock = Mock('mock')
>>> mock.expect_call().will_once(Return('foo'))
<mockify.Expectation: mock()>
>>> mock()
'foo'
__call__(actual_call)

Action body.

It receives actual call object and returns action result based on that call object. This method may also raise exceptions if that is functionality of the action being implemented.

Parameters:actual_call – Instance of mockify.core.Call containing params of actual call being made
format_params(*args, **kwargs)

Used to calculate str() and repr() for this action.

This method should be overloaded in subclass as a no argument method, and then call super().format_params(…) with args and kwargs you want to be included in str() and repr() methods.

class mockify.actions.Iterate(iterable)

Bases: mockify.actions.Action

Similar to Return, but returns an iterator to given iterable.

For example:

>>> from mockify.mock import Mock
>>> from mockify.actions import Iterate
>>> mock = Mock('mock')
>>> mock.expect_call().will_once(Iterate('foo'))
<mockify.Expectation: mock()>
>>> next(mock())
'f'

New in version 0.6.

__call__(actual_call)

Action body.

It receives actual call object and returns action result based on that call object. This method may also raise exceptions if that is functionality of the action being implemented.

Parameters:actual_call – Instance of mockify.core.Call containing params of actual call being made
format_params(*args, **kwargs)

Used to calculate str() and repr() for this action.

This method should be overloaded in subclass as a no argument method, and then call super().format_params(…) with args and kwargs you want to be included in str() and repr() methods.

class mockify.actions.Raise(exc)

Bases: mockify.actions.Action

Forces mock to raise exc when called.

For example:

>>> from mockify.mock import Mock
>>> from mockify.actions import Raise
>>> mock = Mock('mock')
>>> mock.expect_call().will_once(Raise(ValueError('invalid value')))
<mockify.Expectation: mock()>
>>> mock()
Traceback (most recent call last):
    ...
ValueError: invalid value
__call__(actual_call)

Action body.

It receives actual call object and returns action result based on that call object. This method may also raise exceptions if that is functionality of the action being implemented.

Parameters:actual_call – Instance of mockify.core.Call containing params of actual call being made
format_params(*args, **kwargs)

Used to calculate str() and repr() for this action.

This method should be overloaded in subclass as a no argument method, and then call super().format_params(…) with args and kwargs you want to be included in str() and repr() methods.

class mockify.actions.Invoke(func, *args, **kwargs)

Bases: mockify.actions.Action

Forces mock to invoke func when called.

When func is called, it is called with all bound arguments plus all arguments mock was called with. Value that mock returns is the one func returns. Use this action when more sophisticated checks have to be done when mock gets called or when your mock must operate on some output parameter.

See Mocking functions with output parameters for more details.

Here’s an example using one of built-in functions as a func:

>>> from mockify.mock import Mock
>>> from mockify.actions import Invoke
>>> mock = Mock('mock')
>>> mock.expect_call([1, 2, 3]).will_once(Invoke(sum))
<mockify.Expectation: mock([1, 2, 3])>
>>> mock([1, 2, 3])
6

Changed in version 0.6: Now this action allows binding args to function being called.

Parameters:
  • func – Function to be executed
  • args – Additional positional args to be bound to func.
  • kwargs – Additional named args to be bound to func.
__call__(actual_call)

Action body.

It receives actual call object and returns action result based on that call object. This method may also raise exceptions if that is functionality of the action being implemented.

Parameters:actual_call – Instance of mockify.core.Call containing params of actual call being made
format_params(*args, **kwargs)

Used to calculate str() and repr() for this action.

This method should be overloaded in subclass as a no argument method, and then call super().format_params(…) with args and kwargs you want to be included in str() and repr() methods.