對於我們.Net程式員,System.Web.Abstractions我們都非常熟悉,主要作用於Web可以實現單元測試,他是在.Net framework 3.5 sp1開始引入的,很好的解決項目表示層不好做單元測試的問題,這個庫所有類都是Wrapper/Decorator模式的。今天給推薦一個IO ...
對於我們.Net程式員,System.Web.Abstractions我們都非常熟悉,主要作用於Web可以實現單元測試,他是在.Net framework 3.5 sp1開始引入的,很好的解決項目表示層不好做單元測試的問題,這個庫所有類都是Wrapper/Decorator模式的。今天給推薦一個IO的擴展庫與System.Web.Abstractions一樣的,用來支持IO實現單元測試功能。
項目簡介
一個支持IO實現單元測試的擴展庫,支持跨平臺,與File所有API介面都一樣,方便我們項目擴展、遷移。
項目結構
項目主要核心文件是IFileSystem和FileSystem。
技術架構
1、平臺:基於Net4、Netstandard2.0開發
2、開發工具:Visual Studio 2017
使用方法
讀取文本使用例子,使用方法與File類一樣,都是使用相同API名ReadAllText,只是這個API支持可註入和可測試的。
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.");
}
}
}
文件創建單元測試
[Test]
public void Mockfile_Create_ShouldCreateNewStream()
{
string fullPath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"c:\something"));
var sut = new MockFile(fileSystem);
Assert.That(fileSystem.FileExists(fullPath), Is.False);
sut.Create(fullPath).Dispose();
Assert.That(fileSystem.FileExists(fullPath), Is.True);
}
刪除文件單元測試
[Test]
public void MockFile_Delete_ShouldDeleteFile()
{
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test");
var directory = fileSystem.Path.GetDirectoryName(path);
fileSystem.AddFile(path, new MockFileData("Bla"));
var fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length;
fileSystem.File.Delete(path);
var fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length;
Assert.AreEqual(1, fileCount1, "File should have existed");
Assert.AreEqual(0, fileCount2, "File should have been deleted");
}
文件複製單元測試
[Test]
public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue()
{
string sourceFileName = XFS.Path(@"c:\source\demo.txt");
var sourceContents = new MockFileData("Source content");
string destFileName = XFS.Path(@"c:\destination\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{sourceFileName, sourceContents},
{destFileName, new MockFileData("Destination content")}
});
fileSystem.File.Copy(sourceFileName, destFileName, true);
var copyResult = fileSystem.GetFile(destFileName);
Assert.AreEqual(copyResult.Contents, sourceContents.Contents);
}
文件移動單元測試
[Test]
public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem()
{
string sourceFilePath = XFS.Path(@"c:\something\demo.txt");
string sourceFileContent = "this is some content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{sourceFilePath, new MockFileData(sourceFileContent)},
{XFS.Path(@"c:\somethingelse\dummy.txt"), new MockFileData(new byte[] {0})}
});
string destFilePath = XFS.Path(@"c:\somethingelse\demo1.txt");
fileSystem.File.Move(sourceFilePath, destFilePath);
Assert.That(fileSystem.FileExists(destFilePath), Is.True);
Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent));
Assert.That(fileSystem.FileExists(sourceFilePath), Is.False);
}
支持 .NET Framework用法
FileInfo SomeBadApiMethodThatReturnsFileInfo(){return new FileInfo("a");}void MyFancyMethod(){var testableFileInfo = (FileInfoBase)SomeBadApiMethodThatReturnsFileInfo(); ...}
項目地址:https://github.com/Haydabase/System.IO.Abstractions