[issue38757] mocking an exception, arguments do not seem to be passed to the mock

Chris Withers report at bugs.python.org
Sun Nov 10 02:00:43 EST 2019


Chris Withers <chris at withers.org> added the comment:

Not sure this is correct, if an effect is an exception and requires args, then it should be passed as an instance, not a class:

Mock(side_effect=MyException(‘foo’))

> On 10 Nov 2019, at 04:49, Karthikeyan Singaravelan <report at bugs.python.org> wrote:
> 
> 
> Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:
> 
> Currently, the exception is not instantiated. Maybe we can check if it's callable and pass args, kwargs to the exception constructor to be raised.
> 
> diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
> index a48132c5b1..f5bcb911f5 100644
> --- a/Lib/unittest/mock.py
> +++ b/Lib/unittest/mock.py
> @@ -1145,7 +1145,10 @@ class CallableMixin(Base):
>         effect = self.side_effect
>         if effect is not None:
>             if _is_exception(effect):
> -                raise effect
> +                if _callable(effect):
> +                    raise effect(*args, **kwargs)
> +                else:
> +                    raise effect
>             elif not _callable(effect):
>                 result = next(effect)
>                 if _is_exception(result):
> 
> ----------
> nosy: +cjw296, lisroach, mariocj89, michael.foord
> 
> _______________________________________
> Python tracker <report at bugs.python.org>
> <https://bugs.python.org/issue38757>
> _______________________________________

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38757>
_______________________________________


More information about the Python-bugs-list mailing list