[TIP] Problem with mock

Julian Berman julian at grayvines.com
Thu Dec 20 12:10:49 PST 2012


Sorry, yes, I see your original code again now, so yeah I sent you on the
wrong track a bit, sorry, (though clearing out some of the clutter
hopefully helps).

So, your error says your object doesn't have a config attr because it
doesn't. All you've done is mocked super so that it returns a mock object,
but that object is not really the superclass of DevBuild, and DevBuild
never runs Build.__init__, and never sets its own config object.

So, if you want to have a config attr, you have to set it, presumably to a
mock object (in addition to patching super or slightly better
Build.__init__ to ensure it isn't run).

Julian

On Thursday, December 20, 2012, Ken Hagler wrote:

> On Dec 20, 2012, at 9:01 AM, Julian Berman <julian at grayvines.com> wrote:
>
> > You need to replace in the module under test (can't link to the docs but
> > there's a helpful article there if you can't figure it out).
> >
> > Something like:
> >
> > with mock.patch("mymodule.super", create=True) as mock_super:
> >    mock_super.return_value.config.whatever = 12
> >     exercise()
> >     assert()
>
> Okay I tried this, so now test_init starts:
>
> with patch('DevBuild.super', create=True) as mock_super:
>             mock_MakeDevBuild = MagicMock()
>             mock_super.return_value.config.MakeDevBuild = mock_MakeDevBuild
>
> This is certainly simpler, but it also produces the same error:
> "AttributeError: 'DevBuild' object has no attribute 'config'"
> --
>                               Ken Hagler
>
> |                   http://www.orange-road.com/                   |
> |   And tho' we are not now that strength which in old days       |
> |   Moved earth and heaven, that which we are, we are --Tennyson  |
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121220/ed03b20c/attachment.htm>


More information about the testing-in-python mailing list