라이브러리의 핵심에는 IFileSystem
과 FileSystem
있습니다. File.ReadAllText
와 같은 메소드를 직접 호출하는 대신 IFileSystem.File.ReadAllText
사용하십시오. 우리는 우리의 주사가 가능하고 테스트 가능하다는 점을 제외하고는 정확히 동일한 API를 가지고 있습니다.
dotnet add package TestableIO.System.IO.Abstractions.Wrappers
참고 :이 Nuget 패키지는 System.IO.Abstractions
로도 게시되지만 접두사를 사용하여 공식 .NET 패키지가 아님을 분명히하는 것이 좋습니다.
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. " ) ;
}
}
}
도서관은 또한 기본 시나리오를 위해 모든 통화를 조롱하지 않아도 일련의 테스트 도우미와 함께 제공됩니다. 실제 파일 시스템의 완전한 사본은 아니지만 대부분의 길을 얻을 수 있습니다.
dotnet add package TestableIO.System.IO.Abstractions.TestingHelpers
참고 :이 Nuget 패키지는 System.IO.Abstractions.TestingHelpers
로도 게시되지만 접두사를 사용하여 공식 .NET 패키지가 아님을 분명히하는 것이 좋습니다.
[ Test ]
public void MyComponent_Validate_ShouldThrowNotSupportedExceptionIfTestingIsNotAwesome ( )
{
// Arrange
var fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
{
{ @"c:myfile.txt" , new MockFileData ( " Testing is meh. " ) } ,
{ @"c:demojQuery.js" , new MockFileData ( " some js " ) } ,
{ @"c:demoimage.gif" , new MockFileData ( new byte [ ] { 0x12 , 0x34 , 0x56 , 0xd2 } ) }
} ) ;
var component = new MyComponent ( fileSystem ) ;
try
{
// Act
component . Validate ( ) ;
}
catch ( NotSupportedException ex )
{
// Assert
Assert . That ( ex . Message , Is . EqualTo ( " We can't go on together. It's not me, it's you. " ) ) ;
return ;
}
Assert . Fail ( " The expected exception was not thrown. " ) ;
}
우리는 .NET 프레임 워크의 테스트 가능한 유형에서 테스트 가능한 포장지로 캐스팅을 지원합니다.
FileInfo SomeApiMethodThatReturnsFileInfo ( )
{
return new FileInfo ( " a " ) ;
}
void MyFancyMethod ( )
{
var testableFileInfo = ( FileInfoBase ) SomeApiMethodThatReturnsFileInfo ( ) ;
.. .
}
버전 4.0 이후 최상위 API는 추상적 인 기본 클래스 대신 인터페이스를 노출시켜 파일 시스템을 완전히 조롱 할 수 있습니다. 다음은 MOQ를 사용하는 작은 예입니다.
[ Test ]
public void Test1 ( )
{
var watcher = Mock . Of < IFileSystemWatcher > ( ) ;
var file = Mock . Of < IFile > ( ) ;
Mock . Get ( file ) . Setup ( f => f . Exists ( It . IsAny < string > ( ) ) ) . Returns ( true ) ;
Mock . Get ( file ) . Setup ( f => f . ReadAllText ( It . IsAny < string > ( ) ) ) . Throws < OutOfMemoryException > ( ) ;
var unitUnderTest = new SomeClassUsingFileSystemWatcher ( watcher , file ) ;
Assert . Throws < OutOfMemoryException > ( ( ) => {
Mock . Get ( watcher ) . Raise ( w => w . Created += null , new System . IO . FileSystemEventArgs ( System . IO . WatcherChangeTypes . Created , @"C:SomeDirectory" , " Some.File " ) ) ;
} ) ;
Mock . Get ( file ) . Verify ( f => f . Exists ( It . IsAny < string > ( ) ) , Times . Once ) ;
Assert . True ( unitUnderTest . FileWasCreated ) ;
}
public class SomeClassUsingFileSystemWatcher
{
private readonly IFileSystemWatcher _watcher ;
private readonly IFile _file ;
public bool FileWasCreated { get ; private set ; }
public SomeClassUsingFileSystemWatcher ( IFileSystemWatcher watcher , IFile file )
{
this . _file = file ;
this . _watcher = watcher ;
this . _watcher . Created += Watcher_Created ;
}
private void Watcher_Created ( object sender , System . IO . FileSystemEventArgs e )
{
FileWasCreated = true ;
if ( _file . Exists ( e . FullPath ) )
{
var text = _file . ReadAllText ( e . FullPath ) ;
}
}
}
System.IO.Abstractions.Extensions
핵심 추상화 위에 편의 기능을 제공합니다.
System.IO.Abstractions.Analyzers
Roslyn 분석기를 제공하여 정적 방법에 대한 추상화를 사용하는 데 도움이됩니다.
Testably.Abstractions
대체 테스트 도우미 및 추가 추상화를 제공합니다.