Skip to content

Commit

Permalink
Move remarks about context-manager to the Usage page (#383)
Browse files Browse the repository at this point in the history
Fix #382
  • Loading branch information
nicoddemus committed Sep 13, 2023
1 parent 4118e33 commit 0c35b65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
20 changes: 0 additions & 20 deletions docs/remarks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,3 @@ to improve the flow of the test:
# ...
But this is arguably a little more complex than using ``pytest-mock``.

Usage as context manager
------------------------

Although mocker's API is intentionally the same as ``mock.patch``'s, its use
as context manager and function decorator is **not** supported through the
fixture:

.. code-block:: python
def test_context_manager(mocker):
a = A()
with mocker.patch.object(a, 'doIt', return_value=True, autospec=True): # DO NOT DO THIS
assert a.doIt() == True
The purpose of this plugin is to make the use of context managers and
function decorators for mocking unnecessary, so it will emit a warning when used as such.

If you really intend to mock a context manager, ``mocker.patch.context_manager`` exists
which won't issue the above warning.
21 changes: 21 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,24 @@ It may receive an optional name that is shown in its ``repr``, useful for debugg
.. seealso::

``async_stub`` method, which actually the same as ``stub`` but makes async stub.


Usage as context manager
------------------------

Although mocker's API is intentionally the same as ``mock.patch``'s, its use
as context manager and function decorator is **not** supported through the
fixture:

.. code-block:: python
def test_context_manager(mocker):
a = A()
with mocker.patch.object(a, 'doIt', return_value=True, autospec=True): # DO NOT DO THIS
assert a.doIt() == True
The purpose of this plugin is to make the use of context managers and
function decorators for mocking unnecessary, so it will emit a warning when used as such.

If you really intend to mock a context manager, ``mocker.patch.context_manager`` exists
which won't issue the above warning.

0 comments on commit 0c35b65

Please sign in to comment.