I usually mock a class by using autospec parameter so the specs of the class remains except it&#39;s MOCKed.<div>That allows me to just quickly mock out the class I want while keeping all the necessary attributes/method I need.</div>
<div><br></div><div>Also, the error doesn&#39;t indicate it&#39;s a mock object. If it were a mock, I believe you would see &quot;MagicMock object&quot; rather than DevBuild object. </div><div><br></div><div>Just my 2cent. :)</div>
<div><div><br><div class="gmail_quote">On Wed, Dec 19, 2012 at 3:45 PM, Ken Hagler <span dir="ltr">&lt;<a href="mailto:khagler@orange-road.com" target="_blank">khagler@orange-road.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I&#39;ve run into a problem using the mock library. It&#39;s probably something obvious, but I just can&#39;t see it. I&#39;m testing this:<br>
<br>
def __init__(self, buildNum, configFile = &quot;configfile.txt&quot;):<br>
        super(DevBuild, self).__init__(buildNum, configFile)<br>
<br>
        if configFile == &quot;configfile.txt&quot;:<br>
            self.config.MakeDevBuild()<br>
<br>
The superclass&#39;s (called Build) __init__ assigns an instance of another class (Config) to the self.config attribute. I don&#39;t want to run the real Build.__init__, so I&#39;m attempting to create a mock object which has a config attribute which is another mock, which in turn has a MakeDevBuild attribute which is yet another mock.<br>

<br>
Here&#39;s the best I could come up with:<br>
<br>
class TestInit(TestCase):<br>
    def test_init(self):<br>
        old_super = __builtin__.super<br>
<br>
        mock_Config = MagicMock()<br>
        mock_Config.MakeDevBuild = MagicMock()<br>
        mock_Build = MagicMock()<br>
        mock_Build.config = mock_Config<br>
        __builtin__.super = mock_Build<br>
<br>
        # Test with manual configuration<br>
        self.testBuild = DevBuild(&quot;42&quot;, &quot;devconfigfile.txt&quot;)<br>
        self.assertFalse(mock_Config.MakeDevBuild.called)<br>
<br>
        # Test with automated configuration<br>
        self.testBuild = DevBuild(&quot;42&quot;)<br>
        mock_Config.MakeDevBuild.assert_called_once_with()<br>
<br>
        __builtin__.super = old_super<br>
<br>
However, this produces an error:<br>
<br>
Error<br>
Traceback (most recent call last):<br>
  File &quot;/Users/khagler/Projects/BuildClass/BuildClass/test_devBuild.py&quot;, line 22, in test_init<br>
    self.testBuild = DevBuild(&quot;42&quot;)<br>
  File &quot;/Users/khagler/Projects/BuildClass/BuildClass/DevBuild.py&quot;, line 39, in __init__<br>
    self.config.MakeDevBuild()<br>
AttributeError: &#39;DevBuild&#39; object has no attribute &#39;config&#39;<br>
<br>
So I&#39;ve got as far as getting a mock DevBuild object, but the config attribute is missing. Can anyone see what I&#39;m missing?<br>
<span class="HOEnZb"><font color="#888888">--<br>
                              Ken Hagler<br>
<br>
|                   <a href="http://www.orange-road.com/" target="_blank">http://www.orange-road.com/</a>                   |<br>
|   And tho&#39; we are not now that strength which in old days       |<br>
|   Moved earth and heaven, that which we are, we are --Tennyson  |<br>
<br>
<br>
_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
</font></span></blockquote></div><br></div></div>