问题描述
限时送ChatGPT账号..我正在寻找说明如何使用twisted.web.client.Agent 对发出HTTP 请求的代码进行单元测试的提示或示例.
I'm looking for hints or examples that illustrate how to unit test code that makes HTTP requests using twisted.web.client.Agent.
是否可以在测试中使用真实的代理并将其配置为使用 StringTransport(没有真正的 TCP 连接)连接到虚假的 HTTP 服务器?
Is it possible to use the real Agent in tests and configure it to connect to a fake HTTP server using StringTransport (no real TCP connetion)?
或者模拟出代理并将模拟注入使用代理的类是否更好?
Or is it better to mock out the Agent and inject a mock into classes that use the Agent?
推荐答案
当然可以使用真正的 Agent
.您必须使用反应器作为第一个参数来构造 Agent
实例;因此,您可以提供一个假反应器,例如 MemoryReactor
.尽管这是深入了解 Agent
正在做什么的一种非常方便的方法,但它对于测试 Agent
本身确实非常有用.注入一个伪造的 Agent
是一种更简单的方法,因为它的 api 很浅(几乎所有东西都通过 Agent.request
传递,而且很简单(它返回一个 IResponse
)
it is certainly possible to use a real Agent
. You must construct the Agent
instance with a reactor as first argument; as such, you can provide a fake reactor, such as MemoryReactor
. Although that's a pretty handy way to get down in the guts of what Agent
is doing, it's really mostly useful for testing Agent
itself. Injecting a fake Agent
is many times an easier route, because its api is both shallow (almost everything passes through Agent.request
and simple (it returns an IResponse
)
这篇关于如何对使用twisted.web.client.Agent 发出HTTP 请求的代码进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论