I'm trying to mock out urllib.request.urlopen's Read method on Python 3:
Function Code:
try:
with request.urlopen(_webhook_url, json.dumps(_message).encode('utf-8')) as _response:
_response_body = _response.read()
return _response_body
Test Code:
with mock.patch('urllib.request.urlopen') as mock_urlopen:
response_mock = MagicMock()
response_mock.read.return_value = 'ok'
mock_urlopen.return_value = response_mock
with self.stubber:
_response = NotifySlack.lambda_handler(_event)
self.assertEqual('ok', _response)
If I call response_mock.read()
I get the 'ok' value returned, however when I assert the return value I get a mock signature:
Expected :ok
Actual :<MagicMock name='urlopen().__enter__().read()' id='2148156925992'>
Any ideas on why the mock isn't returning the value assigned to read()?