如何开发可测试的 TcpClient/TcpListener Wrapper [英] How can I develop a testable TcpClient / TcpListener Wrapper

查看:20
本文介绍了如何开发可测试的 TcpClient/TcpListener Wrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个可测试的 TcpClient/TcpListener 包装器.我希望能够模拟传入和传出的数据.

i want to develop a testable TcpClient / TcpListener wrapper. I want to be able to mock the incoming and outgoing data.

我想这样做是因为我有更高层的组件可以对网络消息做出反应.出于测试原因,我想模拟(网络)它们.

I want to do this because i have higher tier components that should react on network messages. For testing reasons i want to mock (the network) them.

有人可以给我一个正确的方向吗?

Can some one please give me a kick in the right direction?

推荐答案

您可以使用 装饰模式

  • 制作你自己的类,只包装 TcpClient
  • 这个类只是传递对 TcpClient 中函数的调用.
  • 这个类的重载构造函数可以接受一个实际的 tcp 客户端的实例,如果涉及到创建一个,它将包装它.
  • 提取接口以便您的新类应该实现接口 ITcpClient
  • 更新所有依赖项以使用新接口 ITcpClient
  • 你的新界面现在是可模拟的,注入你的模拟是合适的并进行测试:)

对 TcpServer 重复相同的操作.

Repeat the same for TcpServer.

这篇关于如何开发可测试的 TcpClient/TcpListener Wrapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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