Skip to content
Snippets Groups Projects
Commit 1ff5b468 authored by Michael Foord's avatar Michael Foord
Browse files

Index page docs update

parent ab1df11d
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@
:Homepage: `Mock Homepage`_
:Download: `Mock on PyPI`_
:Documentation: `PDF Documentation
<http://www.voidspace.org.uk/downloads/mock-0.8.0a2.pdf>`_
<http://www.voidspace.org.uk/downloads/mock-0.8.0.pdf>`_
:License: `BSD License`_
:Support: `Mailing list (testing-in-python@lists.idyll.org)
<http://lists.idyll.org/listinfo/testing-in-python>`_
......@@ -50,8 +50,8 @@ Mock is very easy to use and is designed for use with
the 'action -> assertion' pattern instead of `'record -> replay'` used by many
mocking frameworks.
mock is tested on Python versions 2.4-2.7 and Python 3. mock is also tested
with the latest versions of Jython and pypy.
mock is tested on Python versions 2.4-2.7, Python 3 plus the latest versions of
Jython and pypy.
.. testsetup::
......@@ -60,7 +60,7 @@ with the latest versions of Jython and pypy.
def method(self, *args):
pass
test_module = sys.modules['test_module'] = ProductionClass
module = sys.modules['module'] = ProductionClass
ProductionClass.ClassName1 = ProductionClass
ProductionClass.ClassName2 = ProductionClass
......@@ -104,7 +104,7 @@ stabilise). If you find bugs or have suggestions for improvements / extensions
then please contact us.
* `mock on PyPI <http://pypi.python.org/pypi/mock>`_
* `mock documentation as PDF <http://www.voidspace.org.uk/downloads/mock-0.7.2.pdf>`_
* `mock documentation as PDF <http://www.voidspace.org.uk/downloads/mock-0.8.0.pdf>`_
* `Google Code Home & Mercurial Repository <http://code.google.com/p/mock/>`_
.. index:: repository
......@@ -142,14 +142,14 @@ available, and then make assertions about how they have been used:
.. doctest::
>>> from mock import Mock
>>> real = ProductionClass()
>>> real.method = Mock(return_value=3)
>>> real.method(3, 4, 5, key='value')
>>> thing = ProductionClass()
>>> thing.method = Mock(return_value=3)
>>> thing.method(3, 4, 5, key='value')
3
>>> real.method.assert_called_with(3, 4, 5, key='value')
>>> thing.method.assert_called_with(3, 4, 5, key='value')
:attr:`side_effect` allows you to perform side effects, return different
values or raise an exception when a mock is called:
:attr:`side_effect` allows you to perform side effects, including raising an
exception when a mock is called:
.. doctest::
......@@ -158,6 +158,7 @@ values or raise an exception when a mock is called:
Traceback (most recent call last):
...
KeyError: 'foo'
>>> values = {'a': 1, 'b': 2, 'c': 3}
>>> def side_effect(arg):
... return values[arg]
......@@ -170,9 +171,9 @@ values or raise an exception when a mock is called:
(5, 4, 3)
Mock has many other ways you can configure it and control its behaviour. For
example the ``spec`` argument configures the mock to take its specification from
another object. Attempting to access attributes or methods on the mock that
don't exist on the spec will fail with an ``AttributeError``.
example the ``spec`` argument configures the mock to take its specification
from another object. Attempting to access attributes or methods on the mock
that don't exist on the spec will fail with an `AttributeError`.
The :func:`patch` decorator / context manager makes it easy to mock classes or
objects in a module under test. The object you specify will be replaced with a
......@@ -181,14 +182,14 @@ mock (or other object) during the test and restored when the test ends:
.. doctest::
>>> from mock import patch
>>> @patch('test_module.ClassName1')
... @patch('test_module.ClassName2')
>>> @patch('module.ClassName1')
... @patch('module.ClassName2')
... def test(MockClass2, MockClass1):
... test_module.ClassName1()
... test_module.ClassName2()
... module.ClassName1()
... module.ClassName2()
... assert MockClass1 is test_module.ClassName1
... assert MockClass2 is test_module.ClassName2
... assert MockClass1 is module.ClassName1
... assert MockClass2 is module.ClassName2
... assert MockClass1.called
... assert MockClass2.called
...
......@@ -199,7 +200,7 @@ mock (or other object) during the test and restored when the test ends:
When you nest patch decorators the mocks are passed in to the decorated
function in the same order they applied (the normal *python* order that
decorators are applied). This means from the bottom up, so in the example
above the mock for `test_module.ClassName2` is passed in first.
above the mock for `module.ClassName2` is passed in first.
With `patch` it matters that you patch objects in the namespace where they
are looked up. This is normally straightforward, but for a quick guide
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment