Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async_stub method #296

Merged
merged 9 commits into from Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/usage.rst
Expand Up @@ -110,3 +110,7 @@ It may receive an optional name that is shown in its ``repr``, useful for debugg

foo(stub)
stub.assert_called_once_with('foo', 'bar')

.. seealso::

``async_stub`` method, which actually the same as ``stub`` but makes async stub.
Comment on lines +113 to +116
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

13 changes: 13 additions & 0 deletions src/pytest_mock/plugin.py
Expand Up @@ -159,6 +159,19 @@ def stub(self, name: Optional[str] = None) -> unittest.mock.MagicMock:
self.mock_module.MagicMock(spec=lambda *args, **kwargs: None, name=name),
)

def async_stub(self, name: Optional[str] = None) -> unittest.mock.AsyncMock:
"""
Create a async stub method. It accepts any arguments. Ideal to register to
callbacks in tests.

:param name: the constructed stub's name as used in repr
:return: Stub object.
"""
return cast(
unittest.mock.AsyncMock,
self.mock_module.AsyncMock(spec=lambda *args, **kwargs: None, name=name),
)

class _Patcher:
"""
Object to provide the same interface as mock.patch, mock.patch.object,
Expand Down