对于我们.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;
// <summary>Create MyComponent with the given fileSystem implementation</summary>public MyComponent(IFileSystem fileSystem) {this.fileSystem = fileSystem; }/// <summary>Create MyComponent</summary>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<string, MockFileData> { {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<string, MockFileData> { {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

最新文章

  1. 从.NET和Java之争谈IT这个行业
  2. 启动Maven项目启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
  3. VC++6.0文件关联问题的解决方法
  4. 模态窗口用webdriver定位不到,可用java+sikuli实现
  5. W3C盒子与IE盒子模型
  6. bit、sbin、sfr、sfr16 区别分析
  7. CSS入门教程——定位(positon)
  8. 分享自制的C#和VB Code互转工具
  9. Android网络开发之OkHttp--基本用法POST
  10. swift 协议传值的实现
  11. Swing入门
  12. python版本与编码的区别
  13. POJ 3468 A Simple Problem with Integers(树状数组区间更新) 续
  14. c++(爬楼梯)
  15. uestc 1703一道更简单的字符串题目
  16. upload.go
  17. seaborn库
  18. 路漫漫其修远兮,吾将上下而求索--2019OKR规划
  19. spark Transformations算子
  20. git 入门教程之配置 git

热门文章

  1. python基础语法&amp;数据类型&amp;运算符
  2. nuxt.js框架 如何打包 build
  3. react项目中如何出现config文件夹
  4. 网络监测工具之Zabbix的搭建与测试方法(二)-- SNMP、OID和MIB概述
  5. latex文档的中文字体设置
  6. Vue+ElementUI+Springboot实现前后端分离的一个demo
  7. [Computer Networks]一个http请求的完成的全过程
  8. PKUSC2022 润摆寄
  9. (4)go-micro微服务proto开发
  10. 【深入浅出Seata原理及实战】「入门基础专题」带你透析认识Seata分布式事务服务的原理和流程(1)