I want to patch the exists()
method of a pathlib.Path
object for a unit test but I have problems getting this to work.
What I am trying to do is this:
from unittest.mock import patch
from pathlib import Path
def test_move_basic():
p = Path('~/test.py')
with patch.object(p, 'exists') as mock_exists:
mock_exists.return_value = True
But it fails with:
AttributeError: 'PosixPath' object attribute 'exists' is read-only
.
Any ideas?