python/mock-1.0.0/docs/sentinel.txt

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 ==========
     2  Sentinel
     3 ==========
     6 .. currentmodule:: mock
     8 .. testsetup::
    10     class ProductionClass(object):
    11         def something(self):
    12             return self.method()
    14     class Test(unittest2.TestCase):
    15         def testSomething(self):
    16             pass
    17     self = Test('testSomething')
    20 .. data:: sentinel
    22     The ``sentinel`` object provides a convenient way of providing unique
    23     objects for your tests.
    25     Attributes are created on demand when you access them by name. Accessing
    26     the same attribute will always return the same object. The objects
    27     returned have a sensible repr so that test failure messages are readable.
    30 .. data:: DEFAULT
    32     The `DEFAULT` object is a pre-created sentinel (actually
    33     `sentinel.DEFAULT`). It can be used by :attr:`~Mock.side_effect`
    34     functions to indicate that the normal return value should be used.
    37 Sentinel Example
    38 ================
    40 Sometimes when testing you need to test that a specific object is passed as an
    41 argument to another method, or returned. It can be common to create named
    42 sentinel objects to test this. `sentinel` provides a convenient way of
    43 creating and testing the identity of objects like this.
    45 In this example we monkey patch `method` to return
    46 `sentinel.some_object`:
    48 .. doctest::
    50     >>> real = ProductionClass()
    51     >>> real.method = Mock(name="method")
    52     >>> real.method.return_value = sentinel.some_object
    53     >>> result = real.method()
    54     >>> assert result is sentinel.some_object
    55     >>> sentinel.some_object
    56     sentinel.some_object

mercurial