API¶
Stubs and mocks¶
- dobles.allow(target)[source]¶
Prepares a target object for a method call allowance (stub). The name of the method to allow should be called as a method on the return value of this function:
allow(foo).bar
Accessing the
bar
attribute will return anAllowance
which provides additional methods to configure the stub.- Parameters:
target (object) – The object that will be stubbed.
- Returns:
An
AllowanceTarget
for the target object.
- dobles.expect(target)[source]¶
Prepares a target object for a method call expectation (mock). The name of the method to expect should be called as a method on the return value of this function:
expect(foo).bar
Accessing the
bar
attribute will return anExpectation
which provides additional methods to configure the mock.- Parameters:
target (object) – The object that will be mocked.
- Returns:
An
ExpectationTarget
for the target object.
- dobles.allow_constructor(target)[source]¶
Set an allowance on a
ClassDouble
constructorThis allows the caller to control what a ClassDouble returns when a new instance is created.
- Parameters:
target (ClassDouble) – The ClassDouble to set the allowance on.
- Returns:
an
Allowance
for the __new__ method.- Raise:
ConstructorDoubleError
if target is not a ClassDouble.
- dobles.expect_constructor(target)[source]¶
Set an expectation on a
ClassDouble
constructor- Parameters:
target (ClassDouble) – The ClassDouble to set the expectation on.
- Returns:
an
Expectation
for the __new__ method.- Raise:
ConstructorDoubleError
if target is not a ClassDouble.
- dobles.patch(target, value)[source]¶
Replace the specified object
- Parameters:
target (str) – A string pointing to the target to patch.
value (object) – The value to replace the target with.
- Returns:
A
Patch
object.
- dobles.patch_class(target)[source]¶
Replace the specified class with a ClassDouble
- Parameters:
target (str) – A string pointing to the target to patch.
values (obj) – Values to return when new instances are created.
- Returns:
A
ClassDouble
object.
- class dobles.allowance.Allowance(target, method_name, caller)[source]¶
An individual method allowance (stub).
- and_raise(exception, *args, **kwargs)[source]¶
Causes the double to raise the provided exception when called.
If provided, additional arguments (positional and keyword) passed to and_raise are used in the exception instantiation.
- Parameters:
exception (Exception) – The exception to raise.
- and_return(*return_values)[source]¶
Set a return value for an allowance
Causes the double to return the provided values in order. If multiple values are provided, they are returned one at a time in sequence as the double is called. If the double is called more times than there are return values, it should continue to return the last value in the list.
- Parameters:
return_values (object) – The values the double will return when called,
- and_return_result_of(return_value)[source]¶
Causes the double to return the result of calling the provided value.
- Parameters:
return_value (any callable object) – A callable that will be invoked to determine the double’s return value.
- class dobles.expectation.Expectation(target, method_name, caller)[source]¶
An individual method expectation (mock).
- with_args(*args, **kwargs)¶
Declares that the double can only be called with the provided arguments.
- Parameters:
args – Any positional arguments required for invocation.
kwargs – Any keyword arguments required for invocation.
- with_no_args()¶
Declares that the double can only be called with no arguments.
Pure dobles¶
- class dobles.InstanceDouble(path, **kwargs)[source]¶
A pure double representing an instance of the target class.
Any kwargs supplied will be set as attributes on the instance that is created.
user = InstanceDouble('myapp.User', name='Bob Barker')
- Parameters:
path (str) – The absolute module path to the class.
Test lifecycle¶
Exceptions¶
- exception dobles.exceptions.ConstructorDoubleError[source]¶
An exception raised when attempting to double the constructor of a non ClassDouble.
- exception dobles.exceptions.MockExpectationError[source]¶
An exception raised when a mock fails verification.
- exception dobles.exceptions.UnallowedMethodCallError[source]¶
An exception raised when an unallowed method call is made on a double.
- exception dobles.exceptions.VerifyingBuiltinDoubleArgumentError[source]¶
An exception raised when attempting to validate arguments of a builtin.
- exception dobles.exceptions.VerifyingDoubleArgumentError[source]¶
An exception raised when attempting to double a method with arguments that do not match the signature of the real method.