10
10
import os, sys, unittest
11
11
if not os.getcwd() in sys.path:
12
12
sys.path.append(os.getcwd())
14
14
from mock import Mock, sentinel
16
16
class ProductionClass(object):
17
17
def something(self):
18
18
return self.method()
20
20
class Test(unittest.TestCase):
21
21
def testSomething(self):
28
The ``sentinel`` object provides a convenient way of providing unique objects for your tests.
30
Attributes are created on demand when you access them by name. Accessing the same attribute will always return the same object. The objects returned have a sensible repr so that test failure messages are readable.
28
The ``sentinel`` object provides a convenient way of providing unique
29
objects for your tests.
31
Attributes are created on demand when you access them by name. Accessing
32
the same attribute will always return the same object. The objects
33
returned have a sensible repr so that test failure messages are readable.
35
The ``DEFAULT`` object is a pre-created sentinel (actually ``sentinel.DEFAULT``). It can be used by :attr:`Mock.side_effect` functions to indicate that the normal
36
return value should be used.
38
The ``DEFAULT`` object is a pre-created sentinel (actually
39
``sentinel.DEFAULT``). It can be used by :attr:`Mock.side_effect`
40
functions to indicate that the normal return value should be used.
41
Sometimes when testing you need to test that a specific object is passed as an argument to another method, or returned. It can be common to create named sentinel objects to test this. ``sentinel`` provides a convenient way of creating and testing the identity of objects like this.
46
Sometimes when testing you need to test that a specific object is passed as an
47
argument to another method, or returned. It can be common to create named
48
sentinel objects to test this. `sentinel` provides a convenient way of
49
creating and testing the identity of objects like this.
43
In this example we monkey patch ``method`` to return ``sentinel.return_value``. We want to test that this is the value returned when we call ``something``:
51
In this example we monkey patch `method` to return
52
`sentinel.some_object`:
47
56
>>> real = ProductionClass()
48
>>> real.method = Mock()
49
>>> real.method.return_value = sentinel.return_value
50
>>> returned = real.something()
51
>>> self.assertEquals(returned, sentinel.return_value, "something returned the wrong value")
53
>>> sentinel.return_value
54
<SentinelObject "return_value">
57
>>> real.method = Mock(name="method")
58
>>> real.method.return_value = sentinel.some_object
59
>>> result = real.method()
60
>>> assert result is sentinel.some_object
61
>>> sentinel.some_object
62
<SentinelObject "some_object">