This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author thinkingmachin6
Recipients thinkingmachin6
Date 2020-07-14.19:03:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1594753439.43.0.455060750875.issue41296@roundup.psfhosted.org>
In-reply-to
Content
I expected the following code to print True:

<pre>
import os
from unittest.mock import call, Mock, patch

parent = Mock()
parent.getenv = patch('os.getenv')
with parent.getenv:
    os.getenv('FOO', 'bar')
expected = [call.getenv('FOO', 'bar')]
print(expected, '==', parent.mock_calls, ':', expected == parent.mock_calls)
</pre>

It works fine if you replace the statement `parent.getenv = patch('os.getenv')` with:
<pre>
parent.getenv = patch('os.getenv', _mock_parent=parent,
                      _mock_new_parent=parent, _mock_new_name='getenv')
</pre>


Background:

I was trying to make assertions about a mocked call within a mocked context generator.

<pre>
# c.py
from . import a
from . import b
def func():
    with a.context('c'):
        b.operation()
</pre>
<pre>
# test_c.py
from package.c import func
from unittest.mock import Mock, call, patch
parent = Mock()
parent.context = patch('package.a.context')
parent.operation = patch('package.b.operation')
with parent.context, parent.operation:
    func()
assert parent.mock_calls == [
   call.context('c'),
   call.context().__enter__(),
   call.operation(),
   call.context().__exit__(None, None, None),
]
</pre>

in other words, to ensure the correct order of the __enter__/__exit__ calls relative to the actual operation. I have my workaround but it's very awkward looking.
History
Date User Action Args
2020-07-14 19:03:59thinkingmachin6setrecipients: + thinkingmachin6
2020-07-14 19:03:59thinkingmachin6setmessageid: <1594753439.43.0.455060750875.issue41296@roundup.psfhosted.org>
2020-07-14 19:03:59thinkingmachin6linkissue41296 messages
2020-07-14 19:03:59thinkingmachin6create