#1301 closed defect

use a mock decorator for test methods that return Deferreds — at Initial Version

Reported by: davidsarah Owned by: somebody
Priority: minor Milestone: undecided
Component: code Version: 1.8.1
Keywords: mock twisted deferred test Cc:
Launchpad Bug:

Description

Using the @mock.patch decorator for Trial test methods that return Deferreds does not work, because the patch will be undone when the method returns synchronously, not when the Deferred chain completes.

We should add a decorator that can be used instead to src/allmydata/test/common_util.py. Something like:

def trialPatch(*patch_args, **patch_kwargs):
    patcher = mock.patch(*patch_args, **patch_kwargs)
    def decorator(f):
        def inner(*test_args, **test_kwargs):
            mock = patcher.__enter__()
            def cleanup(res):
                patcher.__exit__()
                return res
            test_args += (mock,)
            d = f(*test_args, **test_kwargs)
            d.addBoth(cleanup)
            return d
        return inner
    return decorator

This would require mock 0.6, I think. Thanks to ducki2p and voidspace for this code.

Change History (0)

Note: See TracTickets for help on using tickets.