Message373654
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. |
|
Date |
User |
Action |
Args |
2020-07-14 19:03:59 | thinkingmachin6 | set | recipients:
+ thinkingmachin6 |
2020-07-14 19:03:59 | thinkingmachin6 | set | messageid: <1594753439.43.0.455060750875.issue41296@roundup.psfhosted.org> |
2020-07-14 19:03:59 | thinkingmachin6 | link | issue41296 messages |
2020-07-14 19:03:59 | thinkingmachin6 | create | |
|