剛看到這個(gè)Namespace的時(shí)候還以為是.Net Framework里自帶的包,結(jié)果查了一圈無任何結(jié)果。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供鎮(zhèn)康企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為鎮(zhèn)康眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。果斷上Github,一擊即中https://github.com/tathamoddie/System.IO.Abstractions
先翻譯下開發(fā)者給出的簡單說明,今后再慢慢使用
類似于System.Web.Abstractions的用法,System.IO也被擴(kuò)展了,它能針對(duì)IO對(duì)象進(jìn)行訪問
Just like System.Web.Abstractions, but for System.IO. Yay for testable IO access!
只能用NuGet方式下載
NuGet only:
Install-Package System.IO.Abstractions
如果有需要可以下載測(cè)試幫助包
and/or:
Install-Package System.IO.Abstractions.TestingHelpers
本庫最核心的2個(gè)文件是IFileSystem和FileSystem。使用IFileSystem.File.ReadAllText
等方法替換掉之前的File.ReadAllText等方法。其他API也基本完全相同,除了一些我們擴(kuò)展和進(jìn)行測(cè)試的方法。
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."); } } }
這個(gè)庫中還包含了一系列測(cè)試程序,來幫助你熟悉它。雖然它不是一個(gè)成熟的文件系統(tǒng),但是它一定會(huì)給你帶來幫助的。
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框架里不可測(cè)試的類型加入到測(cè)試程序里
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(); //... }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱:IO擴(kuò)展控件(System.IO.Abstractions)-創(chuàng)新互聯(lián)
URL地址:http://jinyejixie.com/article30/ccejpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)、面包屑導(dǎo)航、網(wǎng)站排名、網(wǎng)站制作、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容