~ubuntu-branches/ubuntu/wily/python-mock/wily-proposed

« back to all changes in this revision

Viewing changes to docs/sentinel.txt

  • Committer: Bazaar Package Importer
  • Author(s): Fladischer Michael
  • Date: 2011-08-23 13:13:44 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110823131344-lx8sddj08rlgaryj
Tags: 0.7.2-1
* New upstream release. (Closes: #617799)
* Set PMPT as maintainer and myself as uploader.
* Switch to source format 3.0 (quilt).
* Switch to dh_python2.
* Use pypi.python.org in d/watch.
* Use DEP5 for copyright file.
* Streamline packaging code with wrap-and-sort.
* Bumped Standards-Version to 3.9.2 (no change necessary).
* Move HTML documentation to u/s/d/python-mock/html/.
* Use dh_link to create the documentation source symlinks.
* Set X-P-V to >= 2.4.
* Build for Python3.
* Use dh_sphinxdoc.
* Ship documentation in separate package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
    import os, sys, unittest
11
11
    if not os.getcwd() in sys.path:
12
12
        sys.path.append(os.getcwd())
13
 
        
 
13
 
14
14
    from mock import Mock, sentinel
15
 
    
 
15
 
16
16
    class ProductionClass(object):
17
17
        def something(self):
18
18
            return self.method()
19
 
    
 
19
 
20
20
    class Test(unittest.TestCase):
21
21
        def testSomething(self):
22
22
            pass
25
25
 
26
26
.. data:: sentinel
27
27
 
28
 
    The ``sentinel`` object provides a convenient way of providing unique objects for your tests.
29
 
 
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.
31
 
 
32
 
    
 
28
    The ``sentinel`` object provides a convenient way of providing unique
 
29
    objects for your tests.
 
30
 
 
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.
 
34
 
 
35
 
33
36
.. data:: DEFAULT
34
37
 
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
 
37
42
 
38
43
Sentinel Example
39
44
================
40
45
 
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.
42
50
 
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`:
44
53
 
45
54
.. doctest::
46
55
 
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")
52
 
    
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">
55
63
 
56
64