allows package¶
-
allows.
allow
(mock_subject: unittest.mock.Mock) → allows.grammar.MockExtensionGrammar[source]¶ Prepare to extend a Mock from the Python Standard Library with a SideEffect.
-
allows.
receive_method
(name: str) → allows.grammar.SideEffectBuilderGrammar[source]¶ Start building a side effect on a named method of a Mock.
my_mock = Mock() allow(my_mock).to(receive_method('foo').and_return('bar)) assert my_mock.foo() == 'bar'
-
allows.
return_value
(return_value: Any) → allows.grammar.SideEffectBuilderGrammar[source]¶ Start building a side effect which returns a value when called.
my_mock = Mock() allow(my_mock).to(return_value('fooby')) assert my_mock() == 'fooby'
-
allows.
raise_exception
(raised_exception: Exception) → allows.grammar.SideEffectBuilderGrammar[source]¶ Start building a side effect which raises an exception.
my_mock = Mock() allow(my_mock).to(raise_exception(ValueError)) raised = False try: my_mock() except ValueError: raised = True assert raised
-
allows.
be_called_with
(*args, **kwargs) → allows.grammar.SideEffectBuilderGrammar[source]¶ Start building a side effect which accepts arguments and keyword arguments
my_mock = Mock() allow(my_mock).to(be_called_with('spam', foo='bar').and_return('eggs)) assert my_mock('spam', foo='bar') == 'eggs'
Submodules¶
allows.grammar module¶
-
class
allows.grammar.
MockExtensionGrammar
(mock_subject: unittest.mock.Mock)[source]¶ MockExtensionGrammar is created by the
allow
factory.This enables grammar for creating and binding a mock side effect like:
allow <Mock> to <Have Side Effect>
-
class
allows.grammar.
SideEffectBuilderGrammar
(method_name=None, builder=None)[source]¶ SideEffectBuilderGrammar is initiated by the
return_value
,raise_exception
,receive_method
,be_called_with
,have_effect
factory methods.The grammar is chainable, but a side effect can have only one effect (return, exception, effect) per grammar expression. However, side effects will automatically combine if multiple expressions are applied to the same mock/method.
This enables grammar for building the side effect like:
allow <Mock> to …
be_called_with <Args> on_method <Name> and_return_value <Value>
-
and_return_value
(*return_value)[source]¶ Add a return value (or a list of return values to cycle through). Alias
and_return
.
-
apply_to
(mock_subject: unittest.mock.Mock) → unittest.mock.Mock[source]¶ Apply the built side effect to the given Python Mock.
-
called_with
(*args, **kwargs)[source]¶ Specify call args that trigger the side effect. Alias
when_called_with
.
-
allows.side_effect module¶
-
class
allows.side_effect.
SideEffect
(call_args=None, effect=None, default_effect=None)[source]¶ Callable object that can compose many side effects with corresponding arguments or default response.