def test_mock_patch_dict_resetall(mocker: MockerFixture) -> None: """ We can call resetall after patching a dict. :param mock: """ x = {"original": 1} mocker.patch.dict(x, values=[("new", 10)], clear=True) assert x == {"new": 10} mocker.resetall() assert x == {"new": 10}
def test_mocker_resetall(mocker: MockerFixture) -> None: listdir = mocker.patch("os.listdir") open = mocker.patch("os.open") listdir("/tmp") open("/tmp/foo.txt") listdir.assert_called_once_with("/tmp") open.assert_called_once_with("/tmp/foo.txt") mocker.resetall() assert not listdir.called assert not open.called
def test_mock_patches( mock_fs: Any, mocker: MockerFixture, check_unix_fs_mocked: Callable[[Any, Any], None], ) -> None: """ Installs mocks into `os` functions and performs a standard testing of mock functionality. We parametrize different mock methods to ensure all (intended, at least) mock API is covered. """ # mock it twice on purpose to ensure we unmock it correctly later mock_fs(mocker) mocked_rm, mocked_ls = mock_fs(mocker) check_unix_fs_mocked(mocked_rm, mocked_ls) mocker.resetall() mocker.stopall()
def test__main(mocker: MockerFixture) -> None: mock_create_config_parser = mocker.patch( "arrsync.__main__.create_config_parser") mock_parse_args = mocker.patch("arrsync.__main__.parse_args") mock_set_debug_level = mocker.patch("arrsync.__main__.set_debug_level") mock_cli_main = mocker.patch("arrsync.__main__.cli.main") spy = mocker.spy(mock_create_config_parser, "read_file") mock_create_config_parser.return_value = mock_create_config_parser mock_parse_args.return_value = Namespace(config="config.conf", debug=False, dry_run=False) main(["--config", "config.conf"]) mock_create_config_parser.assert_called_once() spy.assert_called_once_with("config.conf") mock_cli_main.assert_called_once_with(mock_create_config_parser, False) mocker.resetall() mock_parse_args.return_value = Namespace(config="config.conf", debug=True, dry_run=False) main(["--config", "config.conf", "--debug"]) mock_set_debug_level.assert_called_once_with(logging.DEBUG) mocker.resetall() mock_parse_args.return_value = Namespace(config="config.conf", debug=False, dry_run=True) main(["--config", "config.conf", "--dry-run"]) mock_cli_main.assert_called_once_with(mock_create_config_parser, True)