我正在尝试模拟一堂课。
我试图模拟的类如下所示(为简洁起见,已删除行):
class Connection(object):
"""Connection.
"""
def __init__(self, base_url=None, creds=None, user_agent=None):
self.clients = ClientFactory(self)
如您所见,它有一个名为 的属性clients
。
我的测试方法:
def _auth(self, credentials):
connection = Connection(base_url=f'https://someurl.com', creds=credentials)
return connection.clients
我的单元测试如下所示:
@patch('connection.Connection.__init__')
def test_deploy(patched_connection, fs):
patched_connection.return_value = None
patched_connection.clients = None
# Do some stuff
问题是......我如何clients
在我的测试中设置属性,因为被测方法需要设置它?(我可以将它设置为None
,但我只需要能够设置它。)
使用当前代码,我的应用程序返回错误:
AttributeError: 'Connection' object has no attribute 'clients'
谢谢!