IO Extended Control (System.IO.Abstractions)

Source: Internet
Author: User

When I first saw this namespace, I thought it was a package from the. Net framework, and the result was a lap without any results.

Decisively on GitHub, a hit that's in Https://github.com/tathamoddie/System.IO.Abstractions


First translate the simple instructions given by the developer, and then use it slowly in the future.


Similar to the use of System.Web.Abstractions, System.IO has been extended to access the IO object

Just like System.Web.Abstractions, but for System.IO. Yay for testable IO access!

can only be downloaded in NuGet mode

NuGet only:

Install-package System.IO.Abstractions

Download the test Help pack if you need it

and/or:

Install-package System.IO.Abstractions.TestingHelpers

The Core 2 files of this library are Ifilesystem and filesystem. IFileSystem.File.ReadAllText before replacing it with other methods的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 this 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 contains a series of test programs to help you become familiar with it. Although it is not a mature file system, it will certainly bring you help.

The library also ships with a series of test helpers-to-save you from have to mocks out every call, for basic scenarios. They is not a complete copy of a Real-life file system, but they ' ll get to the most of the the-the-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    &nBsp;   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.");

We even support the. The non-testable types in the NET Framework are added to the test program.

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





This article is from "Shi Zen with. NET & ..." Blog, be sure to keep this source http://stonechan.blog.51cto.com/8414836/1597971

IO Extended Control (System.IO.Abstractions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.