[TIP] How to get magic mock wraps to work with dict attributes?

Darragh Bailey daragh.bailey at gmail.com
Tue Oct 6 07:15:28 PDT 2015


I'm trying to create a mock wrapper around a requests Response object, so
that I can check the calls to both methods and attributes, while still
using the underlying Response object.

However I'm struggling to ensure that access of a custom dict class that is
an attribute of the Response object works as expected and I'm wondering if
I'm missing something or perhaps mock doesn't support my desired usage?

see https://gist.github.com/electrofelix/40f4c38cf28bb701dba3 for an
example with some asserts that ideally should work but fails in membership

The Response object headers attribute, is of type CaseInsensitiveDict, but
there doesn't seem to be an easy way to wrap that object so that various
tests for membership against the mock will work as expected. Though certain
key access works correctly.

My first assumption is I'm doing something wrong, but it might just be a
bug or missing desirable behaviour?

Darragh Bailey
"Nothing is foolproof to a sufficiently talented fool"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20151006/514cf8e1/attachment.htm>

More information about the testing-in-python mailing list