Replacing &quot;@patch.object(g, &#39;meth&#39;, mocksignature=True)&quot; with &quot;@patch(&#39;%s.g.meth&#39; % __name__, mocksignature=True)&quot; produces the same error.  Am I using this wrong?<br><br>Thanks for the clarification regarding spec vs mocksignature.  So if I wanted to do both (a mock class that raises attribute errors when invalid attributes are accessed, but actual method attributes return methods that enforce the correct number of parameters), do I have to set that up manually, or does spec use mocksignature internally when returning attribute values?<br>
<br>Thanks again...<br><br><div class="gmail_quote">On Tue, Nov 2, 2010 at 12:49 PM, Michael Foord <span dir="ltr">&lt;<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">


  
    
  
  <div bgcolor="#ffffff" text="#000000"><div class="im">
    On 02/11/2010 16:43, WW wrote:
    <blockquote type="cite">On Tue, Nov 2, 2010 at 12:17 PM, Michael Foord <span dir="ltr">&lt;<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>&gt;</span>
      wrote:<br>
      <div class="gmail_quote">
        <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
          <div bgcolor="#ffffff" text="#000000">
            <div> On 02/11/2010 15:34, WW wrote:
              <blockquote type="cite">Hello,<br>
                <br>
                This is my first time using the python mock library and
                I&#39;m a little confused.  I&#39;d like to provide some
                guarantees that my mocks are being called with the
                correct number of arguments.  The documentation seems to
                indicate there are two ways to do this, &quot;spec&quot; and
                &quot;mocksignature&quot;, but it&#39;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&#39;m testing use a lot
                of top-level functions from modules they&#39;ve imported. 
                When I do something like this:<br>
                <br>
                @patch.object(somemodule, &#39;somemethod&#39;, spec=True)<br>
                <br>
              </blockquote>
              <br>
            </div>
            You should still be able to use patch with a named function
            (as a string). See the other replies for an example.</div>
        </blockquote>
        <div bgcolor="#ffffff" text="#000000">
          <div><br>
            Does this work if I&#39;m trying to patch a global variable
            rather than a function in another module?<br>
          </div>
        </div>
      </div>
    </blockquote>
    <br></div>
    You mean a global variable in the current module? If so then still
    yes.<br>
    <br>
    @patch(&#39;%s.function&#39; % __name__, mocksignature=True)<br>
    def test_something(self, mockfunction):<br>
        ...<div class="im"><br>
    <br>
    <br>
    <blockquote type="cite">
      <div class="gmail_quote">
        <div bgcolor="#ffffff" text="#000000">
          <div>
            <br>
          </div>
        </div>
        <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
          <div bgcolor="#ffffff" text="#000000">
            <div>
              <blockquote type="cite">
                It doesn&#39;t seem to have any effect; I can call
                somemodule.somemethod with any combination of invalid
                arguments and no exceptions are thrown.  </blockquote>
              <br>
            </div>
            Using spec doesn&#39;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
            &#39;assert_called_with&#39;.
            <div><br>
            </div>
          </div>
        </blockquote>
        <div bgcolor="#ffffff" text="#000000">
          <div><br>
            assert_called_with is fine if I know the exact values I want
            the function to be called with, but I&#39;m just trying to make
            sure that a function was called with the correct number of
            arguments.  If spec can&#39;t do this, then what is spec
            supposed to be used for?  I&#39;m still unclear as to what the
            difference is between spec and mocksignature and why both
            exist.<br>
          </div>
        </div>
      </div>
    </blockquote>
    <br></div>
    spec is not for mocking functions (mocksignature is). Spec is for
    mocking out classes / objects to check that only methods /
    attributes that exist on the spec object are used. Accessing other
    attributes will raise an AttributeError.<div><div></div><div class="h5"><br>
    <br>
    All the best,<br>
    <br>
    Michael Foord<br>
    <br>
    <blockquote type="cite">
      <div class="gmail_quote">
        <div bgcolor="#ffffff" text="#000000">
          <div> </div>
        </div>
        <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
          <div bgcolor="#ffffff" text="#000000">
            <div>
              <blockquote type="cite">
                However, when I do:<br>
                <br>
                @patch.object(somemodule, &#39;somemethod&#39;,
                mocksignature=True)<br>
                <br>
                I get:<br>
                <br>
                Traceback (most recent call last):<br>
                  File &quot;/usr/lib/python2.6/site-
                <div>packages/mock-0.7.0b3-py2.6.egg/mock.py&quot;, line 485,
                  in patched<br>
                      arg = patching.__enter__()<br>
                    File
                  &quot;/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py&quot;,
                  line 536, in __enter__<br>
                      new_attr = mocksignature(original, new)<br>
                    File
                  &quot;/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py&quot;,
                  line 140, in mocksignature<br>
                      signature, func = _getsignature(func, skipfirst)<br>
                    File
                  &quot;/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py&quot;,
                  line 87, in _getsignature<br>
                      func = func.__call__<br>
                  AttributeError: &#39;SentinelObject&#39; object has no
                  attribute &#39;__call__&#39;<br>
                  <br>
                </div>
              </blockquote>
              <br>
            </div>
            This is weird. The traceback implies that you are trying to
            replace a sentinel object using mocksignature (and sentinels
            don&#39;t have signatures to mock). Either that or it is a bug.
            I&#39;ll create a simple test case here (but this functionality
            *is* tested), but it looks like something is not quite setup
            how you expect.</div>
        </blockquote>
        <div bgcolor="#ffffff" text="#000000">
          <div><br>
            Here&#39;s a small test case that produces the error in Python
            2.6, CentOS 5.5:<br>
            <br>
            <div style="font-family: monospace;">
              <ol>
                <li>
                  <div>from mock import patch</div>
                </li>
                <li>
                  <div> </div>
                </li>
                <li>
                  <div>class tc<span>(</span>object<span>)</span>:</div>
                </li>
                <li>
                  <div>    def meth<span>(</span>a,
                    b, c<span>)</span>:</div>
                </li>
                <li>
                  <div>        pass</div>
                </li>
                <li>
                  <div> </div>
                </li>
                <li>
                  <div>g = tc<span>(</span><span>)</span></div>
                </li>
                <li>
                  <div> </div>
                </li>
                <li>
                  <div>@patch.object<span>(</span>g,
                    &#39;meth&#39;, mocksignature=True<span>)</span></div>
                </li>
                <li>
                  <div>def test_g<span>(</span>patched<span>)</span>:</div>
                </li>
                <li>
                  <div>    g.meth<span>(</span><span>1</span><span>)</span></div>
                </li>
                <li>
                  <div> </div>
                </li>
                <li>
                  <div>test_g<span>(</span><span>)</span></div>
                </li>
              </ol>
            </div>
            <br>
          </div>
        </div>
        <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
          <div bgcolor="#ffffff" text="#000000">
            <div> <br>
              All the best,<br>
              <br>
              Michael Foord<br>
            </div>
          </div>
        </blockquote>
        <div bgcolor="#ffffff" text="#000000">
          <div><br>
            Likewise.<br>
          </div>
        </div>
        <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
          <div bgcolor="#ffffff" text="#000000">
            <blockquote type="cite">
              <div>
                <div>What am I missing here?<br>
                  <br>
                  Thanks for your help.</div>
              </div>
              <pre><fieldset></fieldset>
_______________________________________________
testing-in-python mailing list
<div><a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a>
</div></pre>
            </blockquote>
            <div> <br>
              <br>
              <pre cols="72">-- 
<a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a></pre>
            </div>
          </div>
        </blockquote>
      </div>
      <br>
    </blockquote>
    <br>
    <br>
    </div></div><pre cols="72"><div><div></div><div class="h5">-- 

<a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a></div></div>

READ CAREFULLY. By accepting and reading this email you agree,
on behalf of your employer, to release me from all obligations
and waivers arising from any and all NON-NEGOTIATED agreements,
licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap,
confidentiality, non-disclosure, non-compete and acceptable use
policies (”BOGUS AGREEMENTS”) that I have entered into with your
employer, its partners, licensors, agents and assigns, in
perpetuity, without prejudice to my ongoing rights and privileges.
You further represent that you have the authority to release me
from any BOGUS AGREEMENTS on behalf of your employer.
</pre>
  </div>

</blockquote></div><br>