[TIP] Problem with mock

Julian Berman julian at grayvines.com
Thu Dec 20 07:01:21 PST 2012


Hey Ken.

I can't elaborate much from here, but your issue is due to not replacing
super in the correct spot, so your mom isn't being injected properly.

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()

Remembering your code from memory and typing off the cuff, but hopefully
this puts you on your way, otherwise I'm sure someone else can pick it up
from here. Also note the need for create.

Also, doing this is messy. You should give some thought into rethinking
your design. Specifically, separating child from superclass is smelly,
maybe the child ought not be a child at all (and maybe it should compose
with the super).

Anyways, good luck

Julian
On Dec 20, 2012 9:49 AM, "Ken Hagler" <khagler at orange-road.com> wrote:

> On Dec 19, 2012, at 2:57 PM, John Wong <gokoproject at gmail.com> wrote:
>
> > I usually mock a class by using autospec parameter so the specs of the
> > class remains except it's MOCKed.
> > That allows me to just quickly mock out the class I want while keeping
> all
> > the necessary attributes/method I need.
>
> Reasonable, but would I use Build or DevBuild for that? I suppose I'm wary
> of adding another factor that I don't particularly understand when it's
> already not working...
>
> > 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.
>
> Okay, any idea what I'm doing wrong there?
> --
>                               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  |
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121220/4150a10b/attachment.html>


More information about the testing-in-python mailing list