刚看到这个Namespace的时候还以为是.Net Framework里自带的包,结果查了一圈无任何结果。
果断上Github搜索,一击即中 https://github.com/tathamoddie/System.IO.Abstractions
先翻译下开发者给出的简单说明,今后再慢慢使用
类似于System.Web.Abstractions的用法,System.IO也被扩展了,它能针对可测的IO进行访问
Just like System.Web.Abstractions, but for System.IO. Yay for testable IO access!
只能用NuGet方式下载
NuGet only:

 Install-Package System.IO.Abstractions

如果有需要可以下载测试帮助包
and/or:

 Install-Package System.IO.Abstractions.TestingHelpers

本库最核心的2个文件是IFileSystem和FileSystem。使用IFileSystem.File.ReadAllText等方法替换掉之前的File.ReadAllText等方法。除了一些我们扩展和进行测试的方法外,其他API也基本完全相同。
At the core of the library is IFileSystem and FileSystem. Instead of calling methods like File.ReadAllText directly, use IFileSystem.File.ReadAllText. We have exactly the same API, except that ours is injectable and testable.

 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.");
}
}
}

这个库中还包含了一系列测试程序,来帮助你熟悉它。虽然它不是一个成熟的文件系统,但是它一定会给你带来帮助的。
The library also ships with a series of test helpers to save you from having to mock out every call, for basic scenarios. They are not a complete copy of a real-life file system, but they'll get you most of the way there.

 [Test]
public void MyComponent_Validate_ShouldThrowNotSupportedExceptionIfTestingIsNotAwesome()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\myfile.txt", new MockFileData("Testing is meh.") },
{ @"c:\demo\jQuery.js", new MockFileData("some js") },
{ @"c:\demo\image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) }
});
var component = new MyComponent(fileSystem); try
{
// Act
component.Validate();
}
catch (NotSupportedException ex)
{
// Assert
Assert.AreEqual("We can't go on together. It's not me, it's you.", ex.Message);
return;
} Assert.Fail("The expected exception was not thrown.");
}

我们甚至支持把.NET框架里不可测试的类型加入到测试程序里
We even support casting from the .NET Framework's untestable types to our testable wrappers:

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

IO扩展控件(System.IO.Abstractions)的更多相关文章

  1. WPF自定义控件(三)の扩展控件

    扩展控件,顾名思义就是对已有的控件进行扩展,一般继承于已有的原生控件,不排除继承于自定义的控件,不过这样做意义不大,因为既然都自定义了,为什么不一步到位呢,有些不同的需求也可以通过此来完成,不过类似于 ...

  2. 2.C#Panel扩展控件

    1.解决方案下添加新建项目新建类库 2. 在项目下添加新建项选择新建组件类 3.先引用,然后导入两个命名空间 4.因为是扩展控件,把继承自Component改成继承自Panel using Syste ...

  3. 多年前写的文本框扩展控件(有ValueChanging事件等),已放github

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 起因 代码 使用 GitHub ...

  4. 详解C#中System.IO.File类和System.IO.FileInfo类的用法

    System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...

  5. JavaFX的扩展控件库ControlsFX介绍

    声明:   本博客文章原创类别的均为个人原创,版权所有.转载请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http://www.wjfxgame.com. ...

  6. 基类包括字段“ScriptManager1”,但其类型(System.Web.UI.ScriptManager)与控件(System.Web.UI.ScriptManager)的类型不兼容

    首先说下原先的情况,就是原本老项目的Web解决方案是使用.net framework 2.0的老版本, 所以机器也安装过Microsoft ASP.NET 2.0 AJAX Extensions..A ...

  7. IExtenderProvider,c#组件扩展控件属性

    [ProvideProperty("IsEnabled", typeof(LayoutControlItem)), ToolboxItemFilter("System.W ...

  8. 验证控件插图扩展控件ValidatorCalloutExtender(用于扩展验证控件)和TextBoxWatermarkExtender

    <asp:ScriptManager ID="ScriptManager1" runat="server">  </asp:ScriptMan ...

  9. 微软Charting图表控件 System.Web.UI.DataVisuliztion.Charting

    一.概述 基于.NET Framework 3.5 SP1的图表控件--Chart,可在WinForm和WebForm下使用!需要引入System.Web.DataVisualization.dll ...

随机推荐

  1. HDU 5641

    King's Phone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. POJ3177:Redundant Paths(并查集+桥)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19316   Accepted: 8003 ...

  3. mapper.xml配置读取不到

    通常我们在sping的配置文件中,扫描到mapper文件,但是mapper.xml找不到,此时解决办法就是在pom中添加下面代码: <resources> <resource> ...

  4. Leetcode 445. 两数相加 II

    1.题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. ...

  5. 地精排序Gnome Sort

    号称最简单的排序算法,只有一层循环,默认情况下前进冒泡,一旦遇到冒泡的情况发生就往回冒,直到把这个数字放好为止 直接看它排序的过程,待排数组[6 2 4 1 5 9] 先设计一个标识i=0然后从头开始 ...

  6. C++正则表达式例子

    给了策划配置公式的地方,需要将策划配置的公式文本转化为可执行的脚本代码:比如:self->mHp*2+target->2mMp*GetHit()+ self_mon->4mDan/1 ...

  7. Tomcat8利用Redis配置Session共享

    同一个应用在运行多个tomcat实例的时候,经常需要共享Session.tomcat配置共享session有多种方式 1.利用tomcat自身集群特性进行配置: 2.利用Memcache第三方缓存进行 ...

  8. Bower A package manager for the web

    Bower can manage components that contain HTML, CSS, JavaScript, fonts or even image files. Bower doe ...

  9. error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

    error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System. ...

  10. highcharts 从后台动态改变数据

    //columnChart    图表对象,创建示例就展示了. var series = this.columnChart.series;                            whi ...