【发布时间】:2021-01-06 01:56:42
【问题描述】:
我正在尝试模拟以下函数,但我不确定如何模拟连接响应:
def get_user_res(user, pass):
res = None
server = Server('my_server')
connnection = Connection(server, user, pass, strategy=SAFE_SYNC, auto_bind=True)
if connection.bind():
connection.search(search_base, search_filter, SUBTREE)
res = connection.response
connection.unbind()
return res
@mock.patch("ldap3.Server")
@mock.patch("ldap3.Connection.response")
def test_get_user_res(mock_connection, mock_server):
mock_connection.return_value = ""
retrived_res = get_user_res("fake_user","fake_password")
expected_res = ""
assert retrived_res == expected_res
【问题讨论】:
-
如果您在提问时发布minimal, reproducible example 会有所帮助。您发布的代码存在语法错误,并且测试使用了您未显示定义的夹具。这让我们很难提供帮助,因为显而易见的答案是“你用太多的 n 拼写了 'connection'”。
标签: python unit-testing python-unittest python-unittest.mock ldap3