thank you, Michael. following your suggestion, i can mock out the __init__ call, e.g.<br><br>class PymongoConnectionTest(unittest.TestCase):<br>    @patch(&#39;mymodule.pymongo.Connection&#39;)<br>    def test_pymongo_decorator(self, pymongo_connection):<br>
        mymodule.Connection(&#39;localhost&#39;)<br><br>i can also mock base class behavior as you described, e.g.<br><br>    mymodule.Connection.__bases__ = (MockPymongoConnection,)<br><br>was sorely tempted to try Gary&#39;s hack, but will try to erase it from my memory instead :-)<br>
<br>thanks, everyone!<br><br><div class="gmail_quote">On Mon, Jun 20, 2011 at 5:36 PM, Michael Foord <span dir="ltr">&lt;<a href="mailto:michael@voidspace.org.uk">michael@voidspace.org.uk</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<u></u>

  
    
  
  <div bgcolor="#ffffff" text="#000000"><div class="im">
    On 20/06/2011 23:14, Felix Yen wrote:
    <blockquote type="cite">hi, i&#39;m new here and hope my question wasn&#39;t answered
      earlier. (i did look at a bunch of archive threads ...) i derived
      a class from a third-party class; it looks a bit like this:<br>
      <br>
      class Connection(pymongo.Connection):<br>
      <div>    def __init__(self, spam):<br>
                # translate spam ...<br>
                pymongo.Connection.__init__(self, host, port)<br>
        <br>
      </div>
    </blockquote></div>
    If you just want to mock out the call to pymongo.Connection.__init__
    here (and leave your instances effectively uninitialised) then
    patching out pymongo in the same namespace as Connection should then
    allow you to do:<br>
    <br>
        with patch(&#39;mymodule.pymongo&#39; as mock_mongo:<br>
            instance = Connection(&#39;spam&#39;)<br>
    <br>
        mock_mongo.assert_called_once_with(instance, host, port)<br>
    <br>
    If you *actually* want to change the base class of Connection then
    you will have to patch Connection.__bases__ before instantiation.<br>
    <br>
    All the best,<br>
    <br>
    Michael Foord<br>
    <br>
    <br>
    <blockquote type="cite"><div class="im">
      <div>using mock 0.7.2, i wrote a unit test that validates the host
        and port values passed to the base class constructor. however,
        this test fails when nosetests runs it. without nose (1.0.0), i
        can get the test to succeed in two ways:<br>
        <br>
        1) assigning pymongo.Connection to a stub connection class, or<br>
        2) modifying sys.path and importing a fake pymongo package.<br>
        <br>
        mock&#39;s documentation mentions that &quot;<cite>nosetests</cite> does
        some manipulation of <cite>sys.modules</cite> (along with <cite>sys.path</cite>
        manipulation).&quot; so i think this &quot;manipulation&quot; is preventing me
        from patching my connection.<br>
        <br>
      </div>
      has anyone seen this problem before? more generally/importantly,
      what&#39;s a good way of mocking third-party base classes?? i&#39;m using
      Python 2.7 and mock usage is optional (though it&#39;s hard to imagine
      *not* using it), but nose usage is mandatory.<br>
      <br>
      <br>
      Felix<br>
      <br>
      </div><pre><fieldset></fieldset>
_______________________________________________
testing-in-python mailing list
<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>
</pre>
    </blockquote>
    <br>
    <br>
    <pre cols="72">-- 
<a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a>

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a>
</pre>
  </div>

</blockquote></div><br>