mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
347 lines
29 KiB
HTML
347 lines
29 KiB
HTML
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>Mocking Magic Methods — Mock 1.0.0 documentation</title>
|
|
|
|
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
|
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '',
|
|
VERSION: '1.0.0',
|
|
COLLAPSE_INDEX: false,
|
|
FILE_SUFFIX: '.html',
|
|
HAS_SOURCE: true
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="_static/jquery.js"></script>
|
|
<script type="text/javascript" src="_static/underscore.js"></script>
|
|
<script type="text/javascript" src="_static/doctools.js"></script>
|
|
<link rel="top" title="Mock 1.0.0 documentation" href="index.html" />
|
|
<link rel="next" title="Getting Started with Mock" href="getting-started.html" />
|
|
<link rel="prev" title="Sentinel" href="sentinel.html" />
|
|
</head>
|
|
<body>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="getting-started.html" title="Getting Started with Mock"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="sentinel.html" title="Sentinel"
|
|
accesskey="P">previous</a> |</li>
|
|
<li><a href="index.html">Mock 1.0.0 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body">
|
|
|
|
<div class="section" id="mocking-magic-methods">
|
|
<span id="magic-methods"></span><h1>Mocking Magic Methods<a class="headerlink" href="#mocking-magic-methods" title="Permalink to this headline">ΒΆ</a></h1>
|
|
<p><a class="reference internal" href="mock.html#mock.Mock" title="mock.Mock"><tt class="xref py py-class docutils literal"><span class="pre">Mock</span></tt></a> supports mocking <a class="reference external" href="http://www.ironpythoninaction.com/magic-methods.html">magic methods</a>. This allows mock
|
|
objects to replace containers or other objects that implement Python
|
|
protocols.</p>
|
|
<p>Because magic methods are looked up differently from normal methods <a class="footnote-reference" href="#id4" id="id2">[1]</a>, this
|
|
support has been specially implemented. This means that only specific magic
|
|
methods are supported. The supported list includes <em>almost</em> all of them. If
|
|
there are any missing that you need please let us know!</p>
|
|
<p>You mock magic methods by setting the method you are interested in to a function
|
|
or a mock instance. If you are using a function then it <em>must</em> take <tt class="docutils literal"><span class="pre">self</span></tt> as
|
|
the first argument <a class="footnote-reference" href="#id5" id="id3">[2]</a>.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="s">'fooble'</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__str__</span> <span class="o">=</span> <span class="n">__str__</span>
|
|
<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">'fooble'</span>
|
|
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__str__</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__str__</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">'fooble'</span>
|
|
<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">'fooble'</span>
|
|
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__iter__</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">return_value</span><span class="o">=</span><span class="nb">iter</span><span class="p">([]))</span>
|
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">[]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>One use case for this is for mocking objects used as context managers in a
|
|
<cite>with</cite> statement:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__enter__</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">return_value</span><span class="o">=</span><span class="s">'foo'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__exit__</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">return_value</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">mock</span> <span class="k">as</span> <span class="n">m</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">m</span> <span class="o">==</span> <span class="s">'foo'</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__enter__</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__exit__</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Calls to magic methods do not appear in <a class="reference internal" href="mock.html#mock.Mock.method_calls" title="mock.Mock.method_calls"><tt class="xref py py-attr docutils literal"><span class="pre">method_calls</span></tt></a>, but they
|
|
are recorded in <a class="reference internal" href="mock.html#mock.Mock.mock_calls" title="mock.Mock.mock_calls"><tt class="xref py py-attr docutils literal"><span class="pre">mock_calls</span></tt></a>.</p>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">If you use the <cite>spec</cite> keyword argument to create a mock then attempting to
|
|
set a magic method that isn’t in the spec will raise an <cite>AttributeError</cite>.</p>
|
|
</div>
|
|
<p>The full list of supported magic methods is:</p>
|
|
<ul class="simple">
|
|
<li><tt class="docutils literal"><span class="pre">__hash__</span></tt>, <tt class="docutils literal"><span class="pre">__sizeof__</span></tt>, <tt class="docutils literal"><span class="pre">__repr__</span></tt> and <tt class="docutils literal"><span class="pre">__str__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__dir__</span></tt>, <tt class="docutils literal"><span class="pre">__format__</span></tt> and <tt class="docutils literal"><span class="pre">__subclasses__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__floor__</span></tt>, <tt class="docutils literal"><span class="pre">__trunc__</span></tt> and <tt class="docutils literal"><span class="pre">__ceil__</span></tt></li>
|
|
<li>Comparisons: <tt class="docutils literal"><span class="pre">__cmp__</span></tt>, <tt class="docutils literal"><span class="pre">__lt__</span></tt>, <tt class="docutils literal"><span class="pre">__gt__</span></tt>, <tt class="docutils literal"><span class="pre">__le__</span></tt>, <tt class="docutils literal"><span class="pre">__ge__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__eq__</span></tt> and <tt class="docutils literal"><span class="pre">__ne__</span></tt></li>
|
|
<li>Container methods: <tt class="docutils literal"><span class="pre">__getitem__</span></tt>, <tt class="docutils literal"><span class="pre">__setitem__</span></tt>, <tt class="docutils literal"><span class="pre">__delitem__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__contains__</span></tt>, <tt class="docutils literal"><span class="pre">__len__</span></tt>, <tt class="docutils literal"><span class="pre">__iter__</span></tt>, <tt class="docutils literal"><span class="pre">__getslice__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__setslice__</span></tt>, <tt class="docutils literal"><span class="pre">__reversed__</span></tt> and <tt class="docutils literal"><span class="pre">__missing__</span></tt></li>
|
|
<li>Context manager: <tt class="docutils literal"><span class="pre">__enter__</span></tt> and <tt class="docutils literal"><span class="pre">__exit__</span></tt></li>
|
|
<li>Unary numeric methods: <tt class="docutils literal"><span class="pre">__neg__</span></tt>, <tt class="docutils literal"><span class="pre">__pos__</span></tt> and <tt class="docutils literal"><span class="pre">__invert__</span></tt></li>
|
|
<li>The numeric methods (including right hand and in-place variants):
|
|
<tt class="docutils literal"><span class="pre">__add__</span></tt>, <tt class="docutils literal"><span class="pre">__sub__</span></tt>, <tt class="docutils literal"><span class="pre">__mul__</span></tt>, <tt class="docutils literal"><span class="pre">__div__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__floordiv__</span></tt>, <tt class="docutils literal"><span class="pre">__mod__</span></tt>, <tt class="docutils literal"><span class="pre">__divmod__</span></tt>, <tt class="docutils literal"><span class="pre">__lshift__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__rshift__</span></tt>, <tt class="docutils literal"><span class="pre">__and__</span></tt>, <tt class="docutils literal"><span class="pre">__xor__</span></tt>, <tt class="docutils literal"><span class="pre">__or__</span></tt>, and <tt class="docutils literal"><span class="pre">__pow__</span></tt></li>
|
|
<li>Numeric conversion methods: <tt class="docutils literal"><span class="pre">__complex__</span></tt>, <tt class="docutils literal"><span class="pre">__int__</span></tt>, <tt class="docutils literal"><span class="pre">__float__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__index__</span></tt> and <tt class="docutils literal"><span class="pre">__coerce__</span></tt></li>
|
|
<li>Descriptor methods: <tt class="docutils literal"><span class="pre">__get__</span></tt>, <tt class="docutils literal"><span class="pre">__set__</span></tt> and <tt class="docutils literal"><span class="pre">__delete__</span></tt></li>
|
|
<li>Pickling: <tt class="docutils literal"><span class="pre">__reduce__</span></tt>, <tt class="docutils literal"><span class="pre">__reduce_ex__</span></tt>, <tt class="docutils literal"><span class="pre">__getinitargs__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__getnewargs__</span></tt>, <tt class="docutils literal"><span class="pre">__getstate__</span></tt> and <tt class="docutils literal"><span class="pre">__setstate__</span></tt></li>
|
|
</ul>
|
|
<p>The following methods are supported in Python 2 but don’t exist in Python 3:</p>
|
|
<ul class="simple">
|
|
<li><tt class="docutils literal"><span class="pre">__unicode__</span></tt>, <tt class="docutils literal"><span class="pre">__long__</span></tt>, <tt class="docutils literal"><span class="pre">__oct__</span></tt>, <tt class="docutils literal"><span class="pre">__hex__</span></tt> and <tt class="docutils literal"><span class="pre">__nonzero__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__truediv__</span></tt> and <tt class="docutils literal"><span class="pre">__rtruediv__</span></tt></li>
|
|
</ul>
|
|
<p>The following methods are supported in Python 3 but don’t exist in Python 2:</p>
|
|
<ul class="simple">
|
|
<li><tt class="docutils literal"><span class="pre">__bool__</span></tt> and <tt class="docutils literal"><span class="pre">__next__</span></tt></li>
|
|
</ul>
|
|
<p>The following methods exist but are <em>not</em> supported as they are either in use by
|
|
mock, can’t be set dynamically, or can cause problems:</p>
|
|
<ul class="simple">
|
|
<li><tt class="docutils literal"><span class="pre">__getattr__</span></tt>, <tt class="docutils literal"><span class="pre">__setattr__</span></tt>, <tt class="docutils literal"><span class="pre">__init__</span></tt> and <tt class="docutils literal"><span class="pre">__new__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__prepare__</span></tt>, <tt class="docutils literal"><span class="pre">__instancecheck__</span></tt>, <tt class="docutils literal"><span class="pre">__subclasscheck__</span></tt>, <tt class="docutils literal"><span class="pre">__del__</span></tt></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section" id="magic-mock">
|
|
<h1>Magic Mock<a class="headerlink" href="#magic-mock" title="Permalink to this headline">ΒΆ</a></h1>
|
|
<p>There are two <cite>MagicMock</cite> variants: <cite>MagicMock</cite> and <cite>NonCallableMagicMock</cite>.</p>
|
|
<dl class="class">
|
|
<dt id="mock.MagicMock">
|
|
<em class="property">class </em><tt class="descname">MagicMock</tt><big>(</big><em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mock.MagicMock" title="Permalink to this definition">ΒΆ</a></dt>
|
|
<dd><p><tt class="docutils literal"><span class="pre">MagicMock</span></tt> is a subclass of <a class="reference internal" href="mock.html#mock.Mock" title="mock.Mock"><tt class="xref py py-class docutils literal"><span class="pre">Mock</span></tt></a> with default implementations
|
|
of most of the magic methods. You can use <tt class="docutils literal"><span class="pre">MagicMock</span></tt> without having to
|
|
configure the magic methods yourself.</p>
|
|
<p>The constructor parameters have the same meaning as for <a class="reference internal" href="mock.html#mock.Mock" title="mock.Mock"><tt class="xref py py-class docutils literal"><span class="pre">Mock</span></tt></a>.</p>
|
|
<p>If you use the <cite>spec</cite> or <cite>spec_set</cite> arguments then <em>only</em> magic methods
|
|
that exist in the spec will be created.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="class">
|
|
<dt id="mock.NonCallableMagicMock">
|
|
<em class="property">class </em><tt class="descname">NonCallableMagicMock</tt><big>(</big><em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mock.NonCallableMagicMock" title="Permalink to this definition">ΒΆ</a></dt>
|
|
<dd><p>A non-callable version of <cite>MagicMock</cite>.</p>
|
|
<p>The constructor parameters have the same meaning as for
|
|
<a class="reference internal" href="#mock.MagicMock" title="mock.MagicMock"><tt class="xref py py-class docutils literal"><span class="pre">MagicMock</span></tt></a>, with the exception of <cite>return_value</cite> and
|
|
<cite>side_effect</cite> which have no meaning on a non-callable mock.</p>
|
|
</dd></dl>
|
|
|
|
<p>The magic methods are setup with <cite>MagicMock</cite> objects, so you can configure them
|
|
and use them in the usual way:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="s">'fish'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__setitem__</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s">'fish'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__getitem__</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">'result'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
|
|
<span class="go">'result'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>By default many of the protocol methods are required to return objects of a
|
|
specific type. These methods are preconfigured with a default return value, so
|
|
that they can be used without you having to do anything if you aren’t interested
|
|
in the return value. You can still <em>set</em> the return value manually if you want
|
|
to change the default.</p>
|
|
<p>Methods and their defaults:</p>
|
|
<ul class="simple">
|
|
<li><tt class="docutils literal"><span class="pre">__lt__</span></tt>: NotImplemented</li>
|
|
<li><tt class="docutils literal"><span class="pre">__gt__</span></tt>: NotImplemented</li>
|
|
<li><tt class="docutils literal"><span class="pre">__le__</span></tt>: NotImplemented</li>
|
|
<li><tt class="docutils literal"><span class="pre">__ge__</span></tt>: NotImplemented</li>
|
|
<li><tt class="docutils literal"><span class="pre">__int__</span></tt> : 1</li>
|
|
<li><tt class="docutils literal"><span class="pre">__contains__</span></tt> : False</li>
|
|
<li><tt class="docutils literal"><span class="pre">__len__</span></tt> : 1</li>
|
|
<li><tt class="docutils literal"><span class="pre">__iter__</span></tt> : iter([])</li>
|
|
<li><tt class="docutils literal"><span class="pre">__exit__</span></tt> : False</li>
|
|
<li><tt class="docutils literal"><span class="pre">__complex__</span></tt> : 1j</li>
|
|
<li><tt class="docutils literal"><span class="pre">__float__</span></tt> : 1.0</li>
|
|
<li><tt class="docutils literal"><span class="pre">__bool__</span></tt> : True</li>
|
|
<li><tt class="docutils literal"><span class="pre">__nonzero__</span></tt> : True</li>
|
|
<li><tt class="docutils literal"><span class="pre">__oct__</span></tt> : ‘1’</li>
|
|
<li><tt class="docutils literal"><span class="pre">__hex__</span></tt> : ‘0x1’</li>
|
|
<li><tt class="docutils literal"><span class="pre">__long__</span></tt> : long(1)</li>
|
|
<li><tt class="docutils literal"><span class="pre">__index__</span></tt> : 1</li>
|
|
<li><tt class="docutils literal"><span class="pre">__hash__</span></tt> : default hash for the mock</li>
|
|
<li><tt class="docutils literal"><span class="pre">__str__</span></tt> : default str for the mock</li>
|
|
<li><tt class="docutils literal"><span class="pre">__unicode__</span></tt> : default unicode for the mock</li>
|
|
<li><tt class="docutils literal"><span class="pre">__sizeof__</span></tt>: default sizeof for the mock</li>
|
|
</ul>
|
|
<p>For example:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">1</span>
|
|
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">0</span>
|
|
<span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">'0x1'</span>
|
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">[]</span>
|
|
<span class="gp">>>> </span><span class="nb">object</span><span class="p">()</span> <span class="ow">in</span> <span class="n">mock</span>
|
|
<span class="go">False</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The two equality method, <cite>__eq__</cite> and <cite>__ne__</cite>, are special (changed in
|
|
0.7.2). They do the default equality comparison on identity, using a side
|
|
effect, unless you change their return value to return something else:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">MagicMock</span><span class="p">()</span> <span class="o">==</span> <span class="mi">3</span>
|
|
<span class="go">False</span>
|
|
<span class="gp">>>> </span><span class="n">MagicMock</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">3</span>
|
|
<span class="go">True</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__eq__</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="bp">True</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">==</span> <span class="mi">3</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>In <cite>0.8</cite> the <cite>__iter__</cite> also gained special handling implemented with a
|
|
side effect. The return value of <cite>MagicMock.__iter__</cite> can be any iterable
|
|
object and isn’t required to be an iterator:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__iter__</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="p">[</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="s">'c'</span><span class="p">]</span>
|
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">['a', 'b', 'c']</span>
|
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">['a', 'b', 'c']</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If the return value <em>is</em> an iterator, then iterating over it once will consume
|
|
it and subsequent iterations will result in an empty list:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__iter__</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">([</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="s">'c'</span><span class="p">])</span>
|
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">['a', 'b', 'c']</span>
|
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">[]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><tt class="docutils literal"><span class="pre">MagicMock</span></tt> has all of the supported magic methods configured except for some
|
|
of the obscure and obsolete ones. You can still set these up if you want.</p>
|
|
<p>Magic methods that are supported but not setup by default in <tt class="docutils literal"><span class="pre">MagicMock</span></tt> are:</p>
|
|
<ul class="simple">
|
|
<li><tt class="docutils literal"><span class="pre">__cmp__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__getslice__</span></tt> and <tt class="docutils literal"><span class="pre">__setslice__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__coerce__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__subclasses__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__dir__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__format__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__get__</span></tt>, <tt class="docutils literal"><span class="pre">__set__</span></tt> and <tt class="docutils literal"><span class="pre">__delete__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__reversed__</span></tt> and <tt class="docutils literal"><span class="pre">__missing__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__reduce__</span></tt>, <tt class="docutils literal"><span class="pre">__reduce_ex__</span></tt>, <tt class="docutils literal"><span class="pre">__getinitargs__</span></tt>, <tt class="docutils literal"><span class="pre">__getnewargs__</span></tt>,
|
|
<tt class="docutils literal"><span class="pre">__getstate__</span></tt> and <tt class="docutils literal"><span class="pre">__setstate__</span></tt></li>
|
|
<li><tt class="docutils literal"><span class="pre">__getformat__</span></tt> and <tt class="docutils literal"><span class="pre">__setformat__</span></tt></li>
|
|
</ul>
|
|
<hr class="docutils" />
|
|
<table class="docutils footnote" frame="void" id="id4" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id2">[1]</a></td><td>Magic methods <em>should</em> be looked up on the class rather than the
|
|
instance. Different versions of Python are inconsistent about applying this
|
|
rule. The supported protocol methods should work with all supported versions
|
|
of Python.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
<table class="docutils footnote" frame="void" id="id5" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id3">[2]</a></td><td>The function is basically hooked up to the class, but each <tt class="docutils literal"><span class="pre">Mock</span></tt>
|
|
instance is kept isolated from the others.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h3><a href="index.html">Table Of Contents</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#">Mocking Magic Methods</a></li>
|
|
<li><a class="reference internal" href="#magic-mock">Magic Mock</a></li>
|
|
</ul>
|
|
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="sentinel.html"
|
|
title="previous chapter">Sentinel</a></p>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="getting-started.html"
|
|
title="next chapter">Getting Started with Mock</a></p>
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="_sources/magicmock.txt"
|
|
rel="nofollow">Show Source</a></li>
|
|
</ul>
|
|
<div id="searchbox" style="display: none">
|
|
<h3>Quick search</h3>
|
|
<form class="search" action="search.html" method="get">
|
|
<input type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
<p class="searchtip" style="font-size: 90%">
|
|
Enter search terms or a module, class or function name.
|
|
</p>
|
|
</div>
|
|
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="getting-started.html" title="Getting Started with Mock"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="sentinel.html" title="Sentinel"
|
|
>previous</a> |</li>
|
|
<li><a href="index.html">Mock 1.0.0 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2007-2012, Michael Foord & the mock team.
|
|
Last updated on Oct 07, 2012.
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
|
|
</div>
|
|
</body>
|
|
</html> |