Did I find the right examples for you? yes no

All Samples(8)  |  Call(4)  |  Derive(0)  |  Import(4)
Set 'obj.attribute' to 'value' and return a callable to restore 'obj'.

If 'attribute' is not set on 'obj' already, then the returned callable
will delete the attribute when called.

:param obj: An object to monkey-patch.
:param attribute: The name of the attribute to patch.
:param value: The value to set 'obj.attribute' to.
:return: A nullary callable that, when run, will restore 'obj' to its
    original state.

        def patch(obj, attribute, value):
    """Set 'obj.attribute' to 'value' and return a callable to restore 'obj'.

    If 'attribute' is not set on 'obj' already, then the returned callable
    will delete the attribute when called.

    :param obj: An object to monkey-patch.
    :param attribute: The name of the attribute to patch.
    :param value: The value to set 'obj.attribute' to.
    :return: A nullary callable that, when run, will restore 'obj' to its
        original state.
    """
    patcher = MonkeyPatcher((obj, attribute, value))
    patcher.patch()
    return patcher.restore
        


src/t/e/testtools-0.9.35/testtools/tests/test_monkey.py   testtools(Download)
from testtools import TestCase
from testtools.matchers import MatchesException, Raises
from testtools.monkey import MonkeyPatcher, patch
 
 
    def test_patch_patches(self):
        # patch(obj, name, value) sets obj.name to value.
        test_object = TestObj()
        patch(test_object, 'foo', 42)
        self.assertEqual(42, test_object.foo)
        test_object = TestObj()
        original = test_object.foo
        cleanup = patch(test_object, 'foo', 42)
        cleanup()
        self.assertEqual(original, test_object.foo)

src/t/e/testtools-HEAD/testtools/tests/test_monkey.py   testtools(Download)
from testtools import TestCase
from testtools.matchers import MatchesException, Raises
from testtools.monkey import MonkeyPatcher, patch
 
 
    def test_patch_patches(self):
        # patch(obj, name, value) sets obj.name to value.
        test_object = TestObj()
        patch(test_object, 'foo', 42)
        self.assertEqual(42, test_object.foo)
        test_object = TestObj()
        original = test_object.foo
        cleanup = patch(test_object, 'foo', 42)
        cleanup()
        self.assertEqual(original, test_object.foo)

src/t/e/testtools-HEAD/testtools/testcase.py   testtools(Download)
    Raises,
    )
from testtools.monkey import patch
from testtools.runtest import RunTest
from testtools.testresult import (

src/t/e/testtools-0.9.35/testtools/testcase.py   testtools(Download)
    Raises,
    )
from testtools.monkey import patch
from testtools.runtest import RunTest
from testtools.testresult import (