thank you, Michael. following your suggestion, i can mock out the __init__ call, e.g.<br><br>class PymongoConnectionTest(unittest.TestCase):<br> @patch('mymodule.pymongo.Connection')<br> def test_pymongo_decorator(self, pymongo_connection):<br>
mymodule.Connection('localhost')<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'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"><<a href="mailto:michael@voidspace.org.uk">michael@voidspace.org.uk</a>></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'm new here and hope my question wasn'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('mymodule.pymongo' as mock_mongo:<br>
instance = Connection('spam')<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's documentation mentions that "<cite>nosetests</cite> does
some manipulation of <cite>sys.modules</cite> (along with <cite>sys.path</cite>
manipulation)." so i think this "manipulation" is preventing me
from patching my connection.<br>
<br>
</div>
has anyone seen this problem before? more generally/importantly,
what's a good way of mocking third-party base classes?? i'm using
Python 2.7 and mock usage is optional (though it'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>