So, having the unittest.mock and the code
with pysftp.Connection(host, username, password, port) as sftp:
sftp.get("filename")
... one can assert Connection.assert_called_with(host, username, password, port)
.
But assertion Connection.assert_called_with(host, username, password, port).get("filename")
fails.
How do I assert that?
I'm assuming that you would like to patch
pysftp.Connection
reference.
Here is an example of how you can do your test:
>>> import pysftp
>>> from mock import *
>>> with patch("pysftp.Connection") as mock_connection:
... with pysftp.Connection("1.2.3.4", "user", "pwd", 12345) as sftp:
... sftp.get("filename")
... mock_connection.assert_called_with("1.2.3.4", "user", "pwd", 12345)
... sftp.get.assert_called_with("filename")
...
<MagicMock name='Connection().__enter__().get()' id='139907730030992'>
Because sftp
object is a MagicMock
you can use it directly. As you can see in the log sftp
is exactly the same of
mock_connection.return_value.__enter__.return_value
Connection
-> mock_connection
()
-> return_value