1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/mock-1.0.0/html/_sources/sentinel.txt Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,58 @@ 1.4 +========== 1.5 + Sentinel 1.6 +========== 1.7 + 1.8 + 1.9 +.. currentmodule:: mock 1.10 + 1.11 +.. testsetup:: 1.12 + 1.13 + class ProductionClass(object): 1.14 + def something(self): 1.15 + return self.method() 1.16 + 1.17 + class Test(unittest2.TestCase): 1.18 + def testSomething(self): 1.19 + pass 1.20 + self = Test('testSomething') 1.21 + 1.22 + 1.23 +.. data:: sentinel 1.24 + 1.25 + The ``sentinel`` object provides a convenient way of providing unique 1.26 + objects for your tests. 1.27 + 1.28 + Attributes are created on demand when you access them by name. Accessing 1.29 + the same attribute will always return the same object. The objects 1.30 + returned have a sensible repr so that test failure messages are readable. 1.31 + 1.32 + 1.33 +.. data:: DEFAULT 1.34 + 1.35 + The `DEFAULT` object is a pre-created sentinel (actually 1.36 + `sentinel.DEFAULT`). It can be used by :attr:`~Mock.side_effect` 1.37 + functions to indicate that the normal return value should be used. 1.38 + 1.39 + 1.40 +Sentinel Example 1.41 +================ 1.42 + 1.43 +Sometimes when testing you need to test that a specific object is passed as an 1.44 +argument to another method, or returned. It can be common to create named 1.45 +sentinel objects to test this. `sentinel` provides a convenient way of 1.46 +creating and testing the identity of objects like this. 1.47 + 1.48 +In this example we monkey patch `method` to return 1.49 +`sentinel.some_object`: 1.50 + 1.51 +.. doctest:: 1.52 + 1.53 + >>> real = ProductionClass() 1.54 + >>> real.method = Mock(name="method") 1.55 + >>> real.method.return_value = sentinel.some_object 1.56 + >>> result = real.method() 1.57 + >>> assert result is sentinel.some_object 1.58 + >>> sentinel.some_object 1.59 + sentinel.some_object 1.60 + 1.61 +