mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-13 23:17:57 +00:00
352 lines
31 KiB
HTML
352 lines
31 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>mocksignature — Mock 0.8.1alpha1 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: '0.8.1alpha1',
|
|
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 0.8.1alpha1 documentation" href="index.html" />
|
|
<link rel="next" title="Getting Started with Mock" href="getting-started.html" />
|
|
<link rel="prev" title="Mocking Magic Methods" href="magicmock.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="magicmock.html" title="Mocking Magic Methods"
|
|
accesskey="P">previous</a> |</li>
|
|
<li><a href="index.html">Mock 0.8.1alpha1 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body">
|
|
|
|
<div class="section" id="mocksignature">
|
|
<h1>mocksignature<a class="headerlink" href="#mocksignature" title="Permalink to this headline">¶</a></h1>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last"><a class="reference internal" href="helpers.html#auto-speccing"><em>Autospeccing</em></a>, added in mock 0.8, is a more advanced version of
|
|
<cite>mocksignature</cite> and can be used for many of the same use cases.</p>
|
|
</div>
|
|
<p>A problem with using mock objects to replace real objects in your tests is that
|
|
<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> can be <em>too</em> flexible. Your code can treat the mock objects in
|
|
any way and you have to manually check that they were called correctly. If your
|
|
code calls functions or methods with the wrong number of arguments then mocks
|
|
don’t complain.</p>
|
|
<p>The solution to this is <cite>mocksignature</cite>, which creates functions with the
|
|
same signature as the original, but delegating to a mock. You can interrogate
|
|
the mock in the usual way to check it has been called with the <em>right</em>
|
|
arguments, but if it is called with the wrong number of arguments it will
|
|
raise a <cite>TypeError</cite> in the same way your production code would.</p>
|
|
<p>Another advantage is that your mocked objects are real functions, which can
|
|
be useful when your code uses
|
|
<a class="reference external" href="http://docs.python.org/library/inspect.html">inspect</a> or depends on
|
|
functions being function objects.</p>
|
|
<dl class="function">
|
|
<dt id="mock.mocksignature">
|
|
<tt class="descname">mocksignature</tt><big>(</big><em>func</em>, <em>mock=None</em>, <em>skipfirst=False</em><big>)</big><a class="headerlink" href="#mock.mocksignature" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Create a new function with the same signature as <cite>func</cite> that delegates
|
|
to <cite>mock</cite>. If <cite>skipfirst</cite> is True the first argument is skipped, useful
|
|
for methods where <cite>self</cite> needs to be omitted from the new function.</p>
|
|
<p>If you don’t pass in a <cite>mock</cite> then one will be created for you.</p>
|
|
<p>Functions returned by <cite>mocksignature</cite> have many of the same attributes
|
|
and assert methods as a mock object.</p>
|
|
<p>The mock is set as the <cite>mock</cite> attribute of the returned function for easy
|
|
access.</p>
|
|
<p><cite>mocksignature</cite> can also be used with classes. It copies the signature of
|
|
the <cite>__init__</cite> method.</p>
|
|
<p>When used with callable objects (instances) it copies the signature of the
|
|
<cite>__call__</cite> method.</p>
|
|
</dd></dl>
|
|
|
|
<p><cite>mocksignature</cite> will work out if it is mocking the signature of a method on
|
|
an instance or a method on a class and do the “right thing” with the <cite>self</cite>
|
|
argument in both cases.</p>
|
|
<p>Because of a limitation in the way that arguments are collected by functions
|
|
created by <cite>mocksignature</cite> they are <em>always</em> passed as positional arguments
|
|
(including defaults) and not keyword arguments.</p>
|
|
<div class="section" id="mocksignature-api">
|
|
<h2>mocksignature api<a class="headerlink" href="#mocksignature-api" title="Permalink to this headline">¶</a></h2>
|
|
<p>Although the objects returned by <cite>mocksignature</cite> api are real function objects,
|
|
they have much of the same api as the <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> class. This includes the
|
|
assert methods:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span> <span class="o">=</span> <span class="n">mocksignature</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">called</span>
|
|
<span class="go">False</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="mi">3</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">called</span>
|
|
<span class="go">True</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">AssertionError: Expected call</span>: <span class="n">mock(1, 2, 4)</span>
|
|
<span class="go">Actual call: mock(1, 2, 3)</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">call_count</span>
|
|
<span class="go">1</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="ne">IndexError</span>
|
|
<span class="gp">>>> </span><span class="n">func2</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">IndexError</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The mock object that is being delegated to is available as the <cite>mock</cite> attribute
|
|
of the function created by <cite>mocksignature</cite>.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">func2</span><span class="o">.</span><span class="n">mock</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call(1, 2, 3), call(4, 5, 6)]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The methods and attributes available on functions returned by <cite>mocksignature</cite>
|
|
are:</p>
|
|
<blockquote>
|
|
<div><a class="reference internal" href="mock.html#mock.Mock.assert_any_call" title="mock.Mock.assert_any_call"><tt class="xref py py-meth docutils literal"><span class="pre">assert_any_call()</span></tt></a>, <a class="reference internal" href="mock.html#mock.Mock.assert_called_once_with" title="mock.Mock.assert_called_once_with"><tt class="xref py py-meth docutils literal"><span class="pre">assert_called_once_with()</span></tt></a>,
|
|
<a class="reference internal" href="mock.html#mock.Mock.assert_called_with" title="mock.Mock.assert_called_with"><tt class="xref py py-meth docutils literal"><span class="pre">assert_called_with()</span></tt></a>, <a class="reference internal" href="mock.html#mock.Mock.assert_has_calls" title="mock.Mock.assert_has_calls"><tt class="xref py py-meth docutils literal"><span class="pre">assert_has_calls()</span></tt></a>,
|
|
<a class="reference internal" href="mock.html#mock.Mock.call_args" title="mock.Mock.call_args"><tt class="xref py py-attr docutils literal"><span class="pre">call_args</span></tt></a>, <a class="reference internal" href="mock.html#mock.Mock.call_args_list" title="mock.Mock.call_args_list"><tt class="xref py py-attr docutils literal"><span class="pre">call_args_list</span></tt></a>,
|
|
<a class="reference internal" href="mock.html#mock.Mock.call_count" title="mock.Mock.call_count"><tt class="xref py py-attr docutils literal"><span class="pre">call_count</span></tt></a>, <a class="reference internal" href="mock.html#mock.Mock.called" title="mock.Mock.called"><tt class="xref py py-attr docutils literal"><span class="pre">called</span></tt></a>,
|
|
<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>, <cite>mock</cite>, <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>,
|
|
<a class="reference internal" href="mock.html#mock.Mock.reset_mock" title="mock.Mock.reset_mock"><tt class="xref py py-meth docutils literal"><span class="pre">reset_mock()</span></tt></a>, <a class="reference internal" href="mock.html#mock.Mock.return_value" title="mock.Mock.return_value"><tt class="xref py py-attr docutils literal"><span class="pre">return_value</span></tt></a>, and
|
|
<a class="reference internal" href="mock.html#mock.Mock.side_effect" title="mock.Mock.side_effect"><tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt></a>.</div></blockquote>
|
|
</div>
|
|
<div class="section" id="example-use">
|
|
<h2>Example use<a class="headerlink" href="#example-use" title="Permalink to this headline">¶</a></h2>
|
|
<div class="section" id="basic-use">
|
|
<h3>Basic use<a class="headerlink" href="#basic-use" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</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">function</span> <span class="o">=</span> <span class="n">mocksignature</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">mock</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">function</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">TypeError</span>: <span class="n"><lambda>() takes at least 2 arguments (0 given)</span>
|
|
<span class="gp">>>> </span><span class="n">function</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">'some value'</span>
|
|
<span class="gp">>>> </span><span class="n">function</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'foo'</span><span class="p">)</span>
|
|
<span class="go">'some value'</span>
|
|
<span class="gp">>>> </span><span class="n">function</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'foo'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="keyword-arguments">
|
|
<h3>Keyword arguments<a class="headerlink" href="#keyword-arguments" title="Permalink to this headline">¶</a></h3>
|
|
<p>Note that arguments to functions created by <cite>mocksignature</cite> are always passed
|
|
in to the underlying mock by position even when called with keywords:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">function</span> <span class="o">=</span> <span class="n">mocksignature</span><span class="p">(</span><span class="n">function</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">function</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="gp">>>> </span><span class="n">function</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">function</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mocking-methods-and-self">
|
|
<h3>Mocking methods and self<a class="headerlink" href="#mocking-methods-and-self" title="Permalink to this headline">¶</a></h3>
|
|
<p>When you use <cite>mocksignature</cite> to replace a method on a class then <cite>self</cite>
|
|
will be included in the method signature - and you will need to include
|
|
the instance when you do your asserts.</p>
|
|
<p>As a curious factor of the way Python (2) wraps methods fetched from a class,
|
|
we can <em>get</em> the <cite>return_value</cite> from a function set on a class, but we can’t
|
|
set it. We have to do this through the exposed <cite>mock</cite> attribute instead:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">SomeClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">SomeClass</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">mocksignature</span><span class="p">(</span><span class="n">SomeClass</span><span class="o">.</span><span class="n">method</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">SomeClass</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">mock</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">TypeError</span>: <span class="n"><lambda>() takes at least 4 arguments (1 given)</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>When you use <cite>mocksignature</cite> on instance methods <cite>self</cite> isn’t included (and we
|
|
can set the <cite>return_value</cite> etc directly):</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">SomeClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">mocksignature</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mocksignature-with-classes">
|
|
<h3>mocksignature with classes<a class="headerlink" href="#mocksignature-with-classes" title="Permalink to this headline">¶</a></h3>
|
|
<p>When used with a class <cite>mocksignature</cite> copies the signature of the <cite>__init__</cite>
|
|
method.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Something</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">MockSomething</span> <span class="o">=</span> <span class="n">mocksignature</span><span class="p">(</span><span class="n">Something</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">instance</span> <span class="o">=</span> <span class="n">MockSomething</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">instance</span> <span class="ow">is</span> <span class="n">MockSomething</span><span class="o">.</span><span class="n">return_value</span>
|
|
<span class="gp">>>> </span><span class="n">MockSomething</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">MockSomething</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">TypeError</span>: <span class="n"><lambda>() takes at least 2 arguments (0 given)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Because the object returned by <cite>mocksignature</cite> is a function rather than a
|
|
<cite>Mock</cite> you lose the other capabilities of <cite>Mock</cite>, like dynamic attribute
|
|
creation.</p>
|
|
</div>
|
|
<div class="section" id="mocksignature-with-callable-objects">
|
|
<h3>mocksignature with callable objects<a class="headerlink" href="#mocksignature-with-callable-objects" title="Permalink to this headline">¶</a></h3>
|
|
<p>When used with a callable object <cite>mocksignature</cite> copies the signature of the
|
|
<cite>__call__</cite> method.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Something</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">spam</span><span class="p">,</span> <span class="n">eggs</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">something</span> <span class="o">=</span> <span class="n">Something</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock_something</span> <span class="o">=</span> <span class="n">mocksignature</span><span class="p">(</span><span class="n">something</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">mock_something</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock_something</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock_something</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">TypeError</span>: <span class="n"><lambda>() takes at least 2 arguments (0 given)</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mocksignature-argument-to-patch">
|
|
<h2>mocksignature argument to patch<a class="headerlink" href="#mocksignature-argument-to-patch" title="Permalink to this headline">¶</a></h2>
|
|
<p><cite>mocksignature</cite> is available as a keyword argument to <a class="reference internal" href="patch.html#mock.patch" title="mock.patch"><tt class="xref py py-func docutils literal"><span class="pre">patch()</span></tt></a> or
|
|
<a class="reference internal" href="patch.html#mock.patch.object" title="mock.patch.object"><tt class="xref py py-func docutils literal"><span class="pre">patch.object()</span></tt></a>. It can be used with functions / methods / classes and
|
|
callable objects.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">SomeClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="nd">@patch.object</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="s">'method'</span><span class="p">,</span> <span class="n">mocksignature</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
|
<span class="gp">... </span><span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">mock_method</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">instance</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="n">mock_method</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="gp">... </span> <span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">mock_method</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">test</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</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="#">mocksignature</a><ul>
|
|
<li><a class="reference internal" href="#mocksignature-api">mocksignature api</a></li>
|
|
<li><a class="reference internal" href="#example-use">Example use</a><ul>
|
|
<li><a class="reference internal" href="#basic-use">Basic use</a></li>
|
|
<li><a class="reference internal" href="#keyword-arguments">Keyword arguments</a></li>
|
|
<li><a class="reference internal" href="#mocking-methods-and-self">Mocking methods and self</a></li>
|
|
<li><a class="reference internal" href="#mocksignature-with-classes">mocksignature with classes</a></li>
|
|
<li><a class="reference internal" href="#mocksignature-with-callable-objects">mocksignature with callable objects</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="reference internal" href="#mocksignature-argument-to-patch">mocksignature argument to patch</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="magicmock.html"
|
|
title="previous chapter">Mocking Magic Methods</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/mocksignature.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="magicmock.html" title="Mocking Magic Methods"
|
|
>previous</a> |</li>
|
|
<li><a href="index.html">Mock 0.8.1alpha1 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2007-2012, Michael Foord & the mock team.
|
|
Last updated on Feb 16, 2012.
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
|
|
</div>
|
|
</body>
|
|
</html> |