刚看到这个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();
//...
}

最新文章

  1. 分享MSSQL、MySql、Oracle的大数据批量导入方法及编程手法细节
  2. MVC数据验证原理及自定义ModelValidatorProvider实现无编译修改验证规则和错误信息
  3. jQuery在HTML文档加载完毕后自动执行某个事件;
  4. JSP:useBean,setProperty的使用
  5. thinkphp中关于rbac的两个session
  6. PVM的安装和编译PVM程序
  7. vue-输入框change事件并获取值
  8. js获取input上传文件名和后缀
  9. Baker Vai LightOJ - 1071 (MCMF)
  10. Java实现二叉树先序,中序,后序,层次遍历
  11. HDU 2054 又见GCD
  12. CI框架与Thinkphp框架的一些区别
  13. Asp.Net正则过滤一个div
  14. Unity Lighting - Choosing a Color Space 选择色彩空间(四)
  15. JAVA类加载器概念与线程类加载器
  16. Hibernate学习11——Hibernate 高级配置(连接池、log4j)
  17. flask 数据迁移
  18. LeetCode 中级 - 重新排序得到的幂(105)
  19. 第一部分shell编程1基础知识
  20. 分别实现数组所有元素相加、相乘、相与——FP 风格

热门文章

  1. Javascript利用递归实现数组的快速排序
  2. 移动端1px边框问题
  3. java线程的3种创建方式及优缺点
  4. 通过eclipse mybatis generater代码生成插件自动生成代码
  5. UVA 11040 Add bricks in the wall
  6. CodeBlocks wrong
  7. jquery checkbox选中状态以及实现全选反选
  8. SVG 基本绘图方法总结
  9. 【BZOJ】1529 [POI2005]ska Piggy banks
  10. Kaggle机器学习之模型集成(stacking)