一个方便IO单元测试的C#扩展库
创始人
2024-01-17 01:04:38
0

对于我们.Net程序员,System.Web.Abstractions我们都非常熟悉,主要作用于Web可以实现单元测试,他是在.Net framework 3.5 sp1开始引入的,很好的解决项目表示层不好做单元测试的问题,这个库所有类都是Wrapper/Decorator模式的。今天给推荐一个IO的扩展库与System.Web.Abstractions一样的,用来支持IO实现单元测试功能。

项目简介

一个支持IO实现单元测试的扩展库,支持跨平台,与File所有API接口都一样,方便我们项目扩展、迁移。

项目结构

图片

项目主要核心文件是IFileSystem和FileSystem。

技术架构

1、平台:基于Net4、Netstandard2.0开发

2、开发工具:Visual Studio 2017

使用方法

读取文本使用例子,使用方法与File类一样,都是使用相同API名ReadAllText,只是这个API支持可注入和可测试的。

public class MyComponent
{
readonly IFileSystem fileSystem;// Create MyComponent with the given fileSystem implementation
public MyComponent(IFileSystem fileSystem){
this.fileSystem = fileSystem;}
/// Create MyComponent
public MyComponent() : this( fileSystem: new FileSystem() //use default implementation which calls System.IO){}public void Validate(){
foreach (var textFile in fileSystem.Directory.GetFiles(@"c:\", "*.txt", SearchOption.TopDirectoryOnly)){
var text = fileSystem.File.ReadAllText(textFile);
if (text != "Testing is awesome.")
throw new NotSupportedException("We can't go on together. It's not me, it's you.");}}
}

文件创建单元测试

        [Test]
public void Mockfile_Create_ShouldCreateNewStream(){
string fullPath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();fileSystem.AddDirectory(XFS.Path(@"c:\something"));var sut = new MockFile(fileSystem);Assert.That(fileSystem.FileExists(fullPath), Is.False);sut.Create(fullPath).Dispose();Assert.That(fileSystem.FileExists(fullPath), Is.True);}

删除文件单元测试

        [Test]
public void MockFile_Delete_ShouldDeleteFile(){
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test");
var directory = fileSystem.Path.GetDirectoryName(path);fileSystem.AddFile(path, new MockFileData("Bla"));var fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length;fileSystem.File.Delete(path);
var fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length;Assert.AreEqual(1, fileCount1, "File should have existed");Assert.AreEqual(0, fileCount2, "File should have been deleted");}

文件复制单元测试

        [Test]
public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue(){
string sourceFileName = XFS.Path(@"c:\source\demo.txt");
var sourceContents = new MockFileData("Source content");
string destFileName = XFS.Path(@"c:\destination\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary{{sourceFileName, sourceContents},{destFileName, new MockFileData("Destination content")}});fileSystem.File.Copy(sourceFileName, destFileName, true);var copyResult = fileSystem.GetFile(destFileName);Assert.AreEqual(copyResult.Contents, sourceContents.Contents);}

文件移动单元测试

 [Test]
public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem(){
string sourceFilePath = XFS.Path(@"c:\something\demo.txt");
string sourceFileContent = "this is some content";
var fileSystem = new MockFileSystem(new Dictionary{{sourceFilePath, new MockFileData(sourceFileContent)},{XFS.Path(@"c:\somethingelse\dummy.txt"), new MockFileData(new byte[] {0})}});string destFilePath = XFS.Path(@"c:\somethingelse\demo1.txt");fileSystem.File.Move(sourceFilePath, destFilePath);Assert.That(fileSystem.FileExists(destFilePath), Is.True);Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent));Assert.That(fileSystem.FileExists(sourceFilePath), Is.False);}

支持 .NET Framework用法

FileInfo SomeBadApiMethodThatReturnsFileInfo(){return new FileInfo("a");}void MyFancyMethod(){var testableFileInfo = (FileInfoBase)SomeBadApiMethodThatReturnsFileInfo();    ...}

项目地址:https://github.com/Haydabase/System.IO.Abstractions

- End -

推荐阅读

  • 推荐一个C#操作SVG图形矢量图的开源项目

  • 一个支持DOCX、PPTX、Html等文件合并、拆分、互相转换的C#开源项目

  • 盘点10个.NetCore实用的开源框架项目

  • 盘点5个C#开发的、可用于个人博客的系统

  • 一个基于Quartz.Net开发的Windows版本的进程监控

专注分享编程知识、热门有用有趣的开源项目

上一篇:抬起你的头

下一篇:一张丢失的扑克牌

相关内容

热门资讯

常用商务英语口语   商务英语是以适应职场生活的语言要求为目的,内容涉及到商务活动的方方面面。下面是小编收集的常用商务...
六年级上册英语第一单元练习题   一、根据要求写单词。  1.dry(反义词)__________________  2.writ...
复活节英文怎么说 复活节英文怎么说?复活节的英语翻译是什么?复活节:Easter;"Easter,anniversar...
2008年北京奥运会主题曲 2008年北京奥运会(第29届夏季奥林匹克运动会),2008年8月8日到2008年8月24日在中华人...
英语道歉信 英语道歉信15篇  在日常生活中,道歉信的使用频率越来越高,通过道歉信,我们可以更好地解释事情发生的...
六年级英语专题训练(连词成句... 六年级英语专题训练(连词成句30题)  1. have,playhouse,many,I,toy,i...
上班迟到情况说明英语   每个人都或多或少的迟到过那么几次,因为各种原因,可能生病,可能因为交通堵车,可能是因为天气冷,有...
小学英语教学论文 小学英语教学论文范文  引导语:英语教育一直都是每个家长所器重的,那么有关小学英语教学论文要怎么写呢...
英语口语学习必看的方法技巧 英语口语学习必看的方法技巧如何才能说流利的英语? 说外语时,我们主要应做到四件事:理解、回答、提问、...
四级英语作文选:Birth ... 四级英语作文范文选:Birth controlSince the Chinese Governmen...
金融专业英语面试自我介绍 金融专业英语面试自我介绍3篇  金融专业的学生面试时,面试官要求用英语做自我介绍该怎么说。下面是小编...
我的李老师走了四年级英语日记... 我的李老师走了四年级英语日记带翻译  我上了五个学期的小学却换了六任老师,李老师是带我们班最长的语文...
小学三年级英语日记带翻译捡玉... 小学三年级英语日记带翻译捡玉米  今天,我和妈妈去外婆家,外婆家有刚剥的`玉米棒上带有玉米籽,好大的...
七年级英语优秀教学设计 七年级英语优秀教学设计  作为一位兢兢业业的人民教师,常常要写一份优秀的教学设计,教学设计是把教学原...
我的英语老师作文 我的英语老师作文(通用21篇)  在日常生活或是工作学习中,大家都有写作文的经历,对作文很是熟悉吧,...
英语老师教学经验总结 英语老师教学经验总结(通用19篇)  总结是指社会团体、企业单位和个人对某一阶段的学习、工作或其完成...
初一英语暑假作业答案 初一英语暑假作业答案  英语练习一(基础训练)第一题1.D2.H3.E4.F5.I6.A7.J8.C...
大学生的英语演讲稿 大学生的英语演讲稿范文(精选10篇)  使用正确的写作思路书写演讲稿会更加事半功倍。在现实社会中,越...
VOA美国之音英语学习网址 VOA美国之音英语学习推荐网址 美国之音网站已经成为语言学习最重要的资源站点,在互联网上还有若干网站...
商务英语期末试卷 Part I Term Translation (20%)Section A: Translate ...