IO扩展控件(System.IO.Abstractions)-创新互联

刚看到这个Namespace的时候还以为是.Net Framework里自带的包,结果查了一圈无任何结果。

成都创新互联是一家专业提供镇康企业网站建设,专注与成都网站建设、成都网站制作H5响应式网站、小程序制作等业务。10年已为镇康众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

果断上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\p_w_picpath.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();
    //...
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前名称:IO扩展控件(System.IO.Abstractions)-创新互联
URL地址:https://www.cdcxhl.com/article30/ccejpo.html

成都网站建设公司_创新互联,为您提供网站维护移动网站建设面包屑导航网站排名网站制作网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司