Note that there are some explanatory texts on larger screens.

plurals
  1. POpython testing a decorated decorator through a mock
    primarykey
    data
    text
    <p> This question is a follow-up to <a href="https://stackoverflow.com/a/1594484/1504046">this brilliant answer</a> on decorators in Python : </p> <p>I use the given "snippet to make any decorator accept generically any argument". </p> <p>Then I have this (here simplified) decorator:</p> <pre class="lang-py prettyprint-override"><code>@decorator_with_args def has_permission_from_kwarg(func, *args, **kwargs): """Decorator to check simple access/view rights by the kwarg.""" def wrapper(*args_1, **kwargs_1): if 'kwarg' in kwargs_1: kwarg = kwargs_1['kwarg'] else: raise HTTP403Error() return func(*args_1, **kwargs_1) return wrapper </code></pre> <ol> <li>Working with this decorator, no problem it does the job very well.</li> <li>Testing a similar decorator that does not require absolutely the kwargs, same outcome.</li> <li><p>But testing this decorator with the following mock does not work:</p> <pre class="lang-py prettyprint-override"><code>def test_can_access_kwarg(self): """Test simple permission decorator.""" func = Mock(return_value='OK') decorated_func = has_permission_from_slug()(func(kwarg=self.kwarg)) # It will raise at the following line, whereas the kwarg is provided... response = decorated_func() self.assertTrue(func.called) self.assertEqual(response, 'OK') </code></pre></li> </ol> <p>It returns me the exception I am raising when I do not have a 'kwarg' keyword-argument...</p> <p>Does anyone has a clue how to test (by mocking would be preferable) such a decorator decorated by another decorator that requires the access to one of the keyword arguments passed to the function ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload