Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
It turns out that this is exactly what autospeccing is for. Using from pathlib import Path from unittest import mock some_path = Path("/some/path/") with mock.patch("pathlib.Path.is_dir", aut...
Answer
#2: Post edited
- It turns out that this is exactly what [autospeccing](https://docs.python.org/3/library/unittest.mock.html#autospeccing) is for. Using
- ```python
- from pathlib import Path
- from unittest import mock
- some_path = Path("/some/path/")
- with mock.patch("pathlib.Path.is_dir", autospec=True) as m_is_dir:
- m_is_dir.side_effect = lambda p: p.name == ""
- print(Path.is_dir(some_path))
- print(some_path.is_dir())
- ```
- works as expected.
It was this link to an [older version of the docs](https://docs.python.org/3.5/library/unittest.mock-examples.html#mocking-unbound-methods) that put me on the right track.
- It turns out that this is exactly what [autospeccing](https://docs.python.org/3/library/unittest.mock.html#autospeccing) is for. Using
- ```python
- from pathlib import Path
- from unittest import mock
- some_path = Path("/some/path/")
- with mock.patch("pathlib.Path.is_dir", autospec=True) as m_is_dir:
- m_is_dir.side_effect = lambda p: p.name == ""
- print(Path.is_dir(some_path))
- print(some_path.is_dir())
- ```
- works as expected.
- It was this [link](https://docs.python.org/3.5/library/unittest.mock-examples.html#mocking-unbound-methods) to an older version of the docs that put me on the right track.
#1: Initial revision
It turns out that this is exactly what [autospeccing](https://docs.python.org/3/library/unittest.mock.html#autospeccing) is for. Using ```python from pathlib import Path from unittest import mock some_path = Path("/some/path/") with mock.patch("pathlib.Path.is_dir", autospec=True) as m_is_dir: m_is_dir.side_effect = lambda p: p.name == "" print(Path.is_dir(some_path)) print(some_path.is_dir()) ``` works as expected. It was this link to an [older version of the docs](https://docs.python.org/3.5/library/unittest.mock-examples.html#mocking-unbound-methods) that put me on the right track.