Building Behaviours

Behaviours determine what values a mock should return or what exception it should raise when interacted with. Use the when helper to create a behaviour builder for a mock. Building behaviours has two steps:

  1. Interact with the behaviour builder in the same way the mock would be interacted with (e.g. just call it to setup some behaviour for a top-level function).
  2. Specify what result should be returned when interacted with in the way specified in step one.

Interaction can be calling a method, calling a function, accessing a property or accessing an item.

# Mocking methods
my_mock.some_method('some_arg')  # 'some_value'

# Mocking top-level functions
my_mock('some_arg')  # 'some_value'

# Mocking properties
my_mock.some_property  # 'some_value'

# Mocking items
my_mock['some_item']  # 'some_value'
my_mock[10]  # 'some_other_value'


Returning Constants

Use then_return to specify a constant value to return.

my_mock.method()  # 'constant'

Returning computed values

Use then_compute to specify a function for computing a return value based off the arguments used.

when(my_mock).method(any_value).then_compute(lambda x: x ** 2)
my_mock.method(4)  # 16

Raising exceptions

Use then_raise to specify an exception to raise.

my_mock.method()  # ValueError

Chaining results

Results can be chained up just by specifying more than one result. When a mock is interacted with more times than results are specified for the last specified result is used again.

my_mock.bind()  # True
my_mock.bind()  # Exception
my_mock.bind()  # Exception


See Asyncio Support for notes on how to return asyncio coroutines and futures.