<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    On 02/11/2010 15:34, WW wrote:
    <blockquote
      cite="mid:AANLkTikvDp7_Xv9w+dMszb=u=55aJESrRyLG6sHGfozk@mail.gmail.com"
      type="cite">Hello,<br>
      <br>
      This is my first time using the python mock library and I'm a
      little confused.&nbsp; I'd like to provide some guarantees that my
      mocks are being called with the correct number of arguments.&nbsp; The
      documentation seems to indicate there are two ways to do this,
      "spec" and "mocksignature", but it's a little unclear to me what
      the difference is supposed to be between them.<br>
      <br>
      I find myself using the @patch.object decorator almost all the
      time, because the modules I'm testing use a lot of top-level
      functions from modules they've imported.&nbsp; When I do something like
      this:<br>
      <br>
      @patch.object(somemodule, 'somemethod', spec=True)<br>
      <br>
    </blockquote>
    <br>
    You should still be able to use patch with a named function (as a
    string). See the other replies for an example.<br>
    <br>
    <blockquote
      cite="mid:AANLkTikvDp7_Xv9w+dMszb=u=55aJESrRyLG6sHGfozk@mail.gmail.com"
      type="cite">It doesn't seem to have any effect; I can call
      somemodule.somemethod with any combination of invalid arguments
      and no exceptions are thrown.&nbsp; </blockquote>
    <br>
    Using spec doesn't protect you against being called with invalid
    arguments. You should get an error when you validate that the calls
    were made correctly when you call 'assert_called_with'.<br>
    <br>
    <blockquote
      cite="mid:AANLkTikvDp7_Xv9w+dMszb=u=55aJESrRyLG6sHGfozk@mail.gmail.com"
      type="cite">However, when I do:<br>
      <br>
      @patch.object(somemodule, 'somemethod', mocksignature=True)<br>
      <br>
      I get:<br>
      <br>
      Traceback (most recent call last):<br>
      &nbsp; File "/usr/lib/python2.6/site-
      <div id=":57">packages/mock-0.7.0b3-py2.6.egg/mock.py", line 485,
        in patched<br>
        &nbsp;&nbsp;&nbsp; arg = patching.__enter__()<br>
        &nbsp; File
        "/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
        line 536, in __enter__<br>
        &nbsp;&nbsp;&nbsp; new_attr = mocksignature(original, new)<br>
        &nbsp; File
        "/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
        line 140, in mocksignature<br>
        &nbsp;&nbsp;&nbsp; signature, func = _getsignature(func, skipfirst)<br>
        &nbsp; File
        "/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
        line 87, in _getsignature<br>
        &nbsp;&nbsp;&nbsp; func = func.__call__<br>
        AttributeError: 'SentinelObject' object has no attribute
        '__call__'<br>
        <br>
      </div>
    </blockquote>
    <br>
    This is weird. The traceback implies that you are trying to replace
    a sentinel object using mocksignature (and sentinels don't have
    signatures to mock). Either that or it is a bug. I'll create a
    simple test case here (but this functionality *is* tested), but it
    looks like something is not quite setup how you expect.<br>
    <br>
    All the best,<br>
    <br>
    Michael Foord<br>
    <br>
    <blockquote
      cite="mid:AANLkTikvDp7_Xv9w+dMszb=u=55aJESrRyLG6sHGfozk@mail.gmail.com"
      type="cite">
      <div id=":57">What am I missing here?<br>
        <br>
        Thanks for your help.</div>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
testing-in-python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
    </blockquote>
    <br>
    <br>
    <pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/">http://www.voidspace.org.uk/</a></pre>
  </body>
</html>