mock对象3:前进!用库和引用屏蔽掉业务对象和mock对象
引言
上次给我领导演示过mock对象,领导觉得很好,不过他觉得这个方案不完美,因为我们通过一个基类对象屏蔽掉了业务对象和mock对象,虽然从接口上看,看不出这两者的区别,但是对于现有程序,为了造出mock对象进行测试,还要先重构程序才能做到这一点,领导觉得不爽,他想要一种完全透明的解决方案,不修改现有的任何一行代码,就能在业务对象和mock对象之间进行切换,我想了一下,觉得似乎可以通过导入不同库来解决,也就是说我写一个动态链接库项目,定义和业务对象具有相同名字,方法,属性的类,由于上次演示的业务对象其实是系统的库(System.Messaging)系统库在.NET的技术框架下是通过引用导入到项目里的(Visual Studio .NET 项目/应用 右键添加应用 在.NET选项卡下选择System.Messaging)因此我们在自己的解决方案里添加一个Messaging动态链接库项目,它完全模拟系统库的行为,然后我们在使用系统库的项目里替换系统的动态链接库从而达到切换业务对象和mock对象的目的
使用业务对象的程序它的驱动模块(main函数)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Queue queue = new Queue(); while (true) { string s = (string)queue.readMessage(typeof(string)); Console.WriteLine(s); } } } }
使用业务对象的模块
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Messaging; namespace ConsoleApplication1 { class Queue { MessageQueue queue = null; public Queue() { string queueName = ".\\Private$\\TEST"; if (MessageQueue.Exists(queueName)) { queue = new MessageQueue(queueName); } else { queue = MessageQueue.Create(queueName, false); queue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl); } } public object readMessage(Type elementType) { //因为真正的处理代码需要elementType可以读出 System.Messaging.Message message = queue.Receive(); message.Formatter = new XmlMessageFormatter(new Type[] { elementType }); return (object)message.Body; } } }
动态链接库代码:模拟业务对象的mock对象
/* 项目名称:Messaging 项目类型:动态链接库 功 能:模拟系统库System.Messaging的行为,别的项目引用该动态链接库就可以当作系统的库使用,主要配合测试 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace System.Messaging { public enum MessageQueueAccessRights { FullControl = 983103 } public class Message { public object Body; public object Formatter; } public class XmlMessageFormatter { public XmlMessageFormatter(Type[] targetTypeNames) { } } public class MessageQueue { private int elementIndex = 0; private String[] elements = { "hello","world"}; public static MessageQueue Create(string queueName, bool transactional) { return new MessageQueue(); } public static bool Exists(string queueName) { return true; } public MessageQueue(string queueName = "") { } public void SetPermissions(string user, MessageQueueAccessRights rights) { //do nothing } public Message Receive() { Message message = new Message(); if (elementIndex < elements.Length) message.Body = elements[elementIndex++]; else System.Threading.Thread.Sleep(1000 * 60 * 60); return message; } } }
END.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述