我想测试一个我写的简单装饰器:
看起来像这样:
#utilities.py
import other_module
def decor(f):
@wraps(f)
def wrapper(*args, **kwds):
other_module.startdoingsomething()
try:
return f(*args, **kwds)
finally:
other_module.enddoingsomething()
return wrapper
然后我用python-mock测试它:
#test_utilities.py
def test_decor(self):
mock_func = Mock()
decorated_func = self.utilities.decor(mock_func)
decorated_func(1,2,3)
self.assertTrue(self.other_module.startdoingsomething.called)
self.assertTrue(self.other_module.enddoingsomething.called)
mock_func.assert_called_with(1,2,3)
但它又回来了:
Traceback (most recent call last):
File "test_utilities.py", line 25, in test_decor
decorated_func = Mock(wraps=self.utilities.decor(mock_func))
File "utilities.py", line 35, in decor
@wraps(f)
File "/usr/lib/python2.7/functools.py", line 33, in update_wrapper
setattr(wrapper, attr, getattr(wrapped, attr))
File "/usr/local/lib/python2.7/dist-packages/mock.py", line 660, in __getattr__
raise AttributeError(name)
AttributeError: __name__
我知道functools.wraps()
只是一个帮手包装器。所以如果我把它拿出去测试就可以了。
我可以让Mock与functools.wraps()玩得很好吗?
Python 2.7.3
答案 0 :(得分:11)
只需给你的模拟属性:
mock_func.__name__ = 'foo'
真的。
演示:
>>> from functools import wraps
>>> from mock import Mock
>>> def decor(f):
... @wraps(f)
... def wrapper(*args, **kwds):
... return f(*args, **kwds)
... return wrapper
...
>>> mock_func = Mock()
>>> decor(mock_func)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in decor
File ".../opt/lib/python2.7/functools.py", line 33, in update_wrapper
setattr(wrapper, attr, getattr(wrapped, attr))
File ".../lib/python2.7/site-packages/mock.py", line 660, in __getattr__
raise AttributeError(name)
AttributeError: __name__
>>> mock_func.__name__ = 'foo'
>>> decor(mock_func)
<function foo at 0x10c4321b8>
设置__name__
非常好; @wraps
装饰器只是将__name__
属性复制到包装器,而在函数对象上,属性通常设置为字符串值。无论如何,它都是函数的可写属性,只要你使用字符串function.__name__
就可以设置为任何值。