<!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 20/06/2011 23:14, Felix Yen wrote:
<blockquote
cite="mid:BANLkTinPVM3LfDDsxPJ+fMVferEqadObWw@mail.gmail.com"
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>
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
cite="mid:BANLkTinPVM3LfDDsxPJ+fMVferEqadObWw@mail.gmail.com"
type="cite">
<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>
<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>
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 class="moz-txt-link-freetext" href="http://www.sqlite.org/different.html">http://www.sqlite.org/different.html</a>
</pre>
</body>
</html>