在 Node.js net.Socket 实例上模拟 ECONNRESET 错误 [英] Simulate an ECONNRESET error on a Node.js net.Socket instance

查看:39
本文介绍了在 Node.js net.Socket 实例上模拟 ECONNRESET 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些单元测试,我想在 net.Socket 实例上模拟连接丢失 (ECONNRESET) 作为其中一项测试的一部分.

I'm writing some unit tests and I'd like to simulate a connection loss (ECONNRESET) on a net.Socket instance as part of one of the tests.

这样做的最佳方法是什么?

What would be the best way to go about doing this?

谢谢

推荐答案

我最后的做法是这样的:

The way I went for in the end was as follows:

client.end();
client.emit('error', new Error('ECONNRESET'));

是我为了测试目的所需要的

was all I needed for the purposes of the test

这篇关于在 Node.js net.Socket 实例上模拟 ECONNRESET 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆