Skip to content

spy.reset_mock(side_effect=True) makes spies stop calling the spied code #636

Open

Description

@nicoddemus

mocker.spy() calls the spied code from the spy's side_effect. #633 made mocker.resetall(side_effect=True) restore it after the reset, but calling reset_mock(side_effect=True) on the spy itself still clears it, so the spy returns a MagicMock from then on:

class Foo:
    @classmethod
    def class_bar(cls, x):
        return x * 3

def test_spy_reset_mock(mocker):
    spy = mocker.spy(Foo, "class_bar")
    spy.reset_mock(side_effect=True)
    assert Foo.class_bar(2) == 6  # fails: returns <MagicMock name='class_bar()' ...>

Reproduced on 3.16.1. It affects spies whose mock is a MagicMock: classmethods, staticmethods, builtins and classes. Spies on functions and methods are autospecced functions whose reset_mock() takes no arguments, so they aren't affected today.

Fixing it means handling the reset on the spy object itself (for example overriding reset_mock on spies), since resetall() is not involved here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions