Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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