我无法确定这个功能的确切位置,因此我不知道提供给修补程序装饰器的路径。
<file b.py>
from a import function_to_mock
def deco(*args, **kwargs):
user = kwargs['user']
...some db access, user validation...
def outer(func):
@functools.wraps(func)
def inner(*args, *kwargs):
bool = function_to_mock(args[0])
if bool:
# inject variable from outermost level into decorated function
kwargs['user'] = user
return func(*args, **kwargs)
else:
raise ValueError('error...')
return inner
return outer
测试代码:
def test_deco(self):
with patch('path.to.b.function_to_mock', lambda x: self.pilot):
@deco(roles=[UserRole.Pilot], getter=some_function)
def func(request, **kwargs):
print('func kwargs %s' % kwargs)
...assertions...
func({'some': '4'})
我试图修补路径.to.a.function_to_mock&#39;和&#39; path.to.b.function_to_mock&#39;但似乎都没有奏效。什么是正确的道路?或者,有没有更好的方法来测试装饰器功能?