为扭曲的应用程序编写单元测试。使用新的(虚拟)连接(实例proto_helpers.StringTransport
)解决了延迟后尝试执行一些断言的问题,但是回调assert_cache_updated_on_connection
收到的是None
而不是原来的connection
传递给<deferred>.callback(connection)
def test_send_to_new_connection(self):
# Given
peerAddr = ('10.22.22.190', 5060)
# If
self.tcp_transport.send_to('test', peerAddr)
# Then
assert peerAddr in self.tcp_transport._connections
assert True == isinstance(self.tcp_transport._connections[peerAddr], Deferred)
connection = _string_transport_connection(self.hostAddr, peerAddr, None, self.tcp_transport.connectionMade)
def assert_cache_updated_on_connection(connection):
print('--------- SUCCESS ----------')
peer = connection.transport.getPeer()
peerAddr = (peer.host, peer.port)
assert peerAddr in self.tcp_transport._connections
assert True == isinstance(self.tcp_transport._connections[peerAddr], Protocol)
def assert_fail(fail):
print('--------- FAIL ----------')
self.tcp_transport._connections[peerAddr].addCallback(assert_cache_updated_on_connection)
self.tcp_transport._connections[peerAddr].addErrback(assert_fail)
# Forcing deferred to fire with mock connection
self.tcp_transport._connections[peerAddr].callback(connection)
这是手动触发延迟回调的正确方法吗?
答案 0 :(得分:1)
回调函数将以下两项之一作为其第一个参数:
callback
方法的值。因此,大概的解释是assert_cache_updated_on_connection
不是第一个回调,并且该回调返回None
之前。