I usually mock a class by using autospec parameter so the specs of the class remains except it'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't indicate it's a mock object. If it were a mock, I believe you would see "MagicMock object" 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"><<a href="mailto:khagler@orange-road.com" target="_blank">khagler@orange-road.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've run into a problem using the mock library. It's probably something obvious, but I just can't see it. I'm testing this:<br>
<br>
def __init__(self, buildNum, configFile = "configfile.txt"):<br>
super(DevBuild, self).__init__(buildNum, configFile)<br>
<br>
if configFile == "configfile.txt":<br>
self.config.MakeDevBuild()<br>
<br>
The superclass's (called Build) __init__ assigns an instance of another class (Config) to the self.config attribute. I don't want to run the real Build.__init__, so I'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'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("42", "devconfigfile.txt")<br>
self.assertFalse(mock_Config.MakeDevBuild.called)<br>
<br>
# Test with automated configuration<br>
self.testBuild = DevBuild("42")<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 "/Users/khagler/Projects/BuildClass/BuildClass/test_devBuild.py", line 22, in test_init<br>
self.testBuild = DevBuild("42")<br>
File "/Users/khagler/Projects/BuildClass/BuildClass/DevBuild.py", line 39, in __init__<br>
self.config.MakeDevBuild()<br>
AttributeError: 'DevBuild' object has no attribute 'config'<br>
<br>
So I've got as far as getting a mock DevBuild object, but the config attribute is missing. Can anyone see what I'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' 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>