IO扩展控件(System.IO.Abstractions)
刚看到这个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)的更多相关文章
- WPF自定义控件(三)の扩展控件
扩展控件,顾名思义就是对已有的控件进行扩展,一般继承于已有的原生控件,不排除继承于自定义的控件,不过这样做意义不大,因为既然都自定义了,为什么不一步到位呢,有些不同的需求也可以通过此来完成,不过类似于 ...
- 2.C#Panel扩展控件
1.解决方案下添加新建项目新建类库 2. 在项目下添加新建项选择新建组件类 3.先引用,然后导入两个命名空间 4.因为是扩展控件,把继承自Component改成继承自Panel using Syste ...
- 多年前写的文本框扩展控件(有ValueChanging事件等),已放github
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 起因 代码 使用 GitHub ...
- 详解C#中System.IO.File类和System.IO.FileInfo类的用法
System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...
- JavaFX的扩展控件库ControlsFX介绍
声明: 本博客文章原创类别的均为个人原创,版权所有.转载请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http://www.wjfxgame.com. ...
- 基类包括字段“ScriptManager1”,但其类型(System.Web.UI.ScriptManager)与控件(System.Web.UI.ScriptManager)的类型不兼容
首先说下原先的情况,就是原本老项目的Web解决方案是使用.net framework 2.0的老版本, 所以机器也安装过Microsoft ASP.NET 2.0 AJAX Extensions..A ...
- IExtenderProvider,c#组件扩展控件属性
[ProvideProperty("IsEnabled", typeof(LayoutControlItem)), ToolboxItemFilter("System.W ...
- 验证控件插图扩展控件ValidatorCalloutExtender(用于扩展验证控件)和TextBoxWatermarkExtender
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptMan ...
- 微软Charting图表控件 System.Web.UI.DataVisuliztion.Charting
一.概述 基于.NET Framework 3.5 SP1的图表控件--Chart,可在WinForm和WebForm下使用!需要引入System.Web.DataVisualization.dll ...
随机推荐
- HDU 3487 Splay tree
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- DEBUG宏
4.8.6.运算中的临时匿名变量4.8.6.1.C语言和汇编的区别(汇编完全对应机器操作,C对应逻辑操作)(1)C语言叫高级语言,汇编语言叫低级语言.(2)低级语言的意思是汇编语言和机器操作相对应,汇 ...
- Web Api Action的筛选
web Api设置默认路由设置: 这种目标Action方法的选择有以下几轮: 1.针对 HTTP方法 进行筛选 2.针对参数类型,可以做参数约束 3.针对参数数量 另一种路由“api/{control ...
- You can't specify target table 'table' for update in FROM clause
delete from table1 where ID not in(select max(ID) ID from table1 group by row1) and row1 ) # 出现错误 # ...
- [POI2008]BLO-Blockade
https://www.luogu.org/problem/show?pid=3469 题目描述 There are exactly towns in Byteotia. Some towns ar ...
- [Luogu 4092] HEOI/TJOI2016 树
[Luogu 4092] HEOI/TJOI2016 树 搜了树剖标签不知道怎么就跳出了个暴搜题啊! 管他既然做了就发上来吧- 有修改标签就向下搜并修改,遇到标签即停止. 这题是真的真的短. #inc ...
- Idea Ant 打开发包
简介: http://ju.outofmemory.cn/entry/19239 语法: https://wenku.baidu.com/view/a0e00315866fb84ae45c8d9d.h ...
- UIImagePickerController---iOS-Apple苹果官方文档翻译
//本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com ...
- 对象方法、类方法、原型方法 && 私有属性、公有属性、公有静态属性
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...