Inline stubbing and mockingΒΆ

New in version 1.8: - Proposed by Carlos Ble

Several pyDoubles users ask for an alternative to set stubbing and mocking in a way similar to pyDoubles API, that is, instead of use the double context manager:

with Stub() as stub:
    stub.foo(1).returns(100)

with Mock() as mock:
    mock.bar(2).returns(50)

You may invoke the when() and expect_call() functions to get the same setup.

stub = Stub()
when(stub).foo(1).returns(100)

mock = Mock()
expect_call(mock).bar(2).returns(50)

Note that when() and expect_call() internally provide almost the same functionality. Two functions are provided only for test readability purposes. when() is intented for stubs, spies and proxyspies, and expect_call() is intented for mocks.