Testably.Abstractions
Testably.Abstractions is a feature-complete testing helper for the IFileSystem abstractions of the System.IO namespace, plus matching abstractions for time and randomness.
It provides:
- An in-memory
MockFileSystemthat behaves exactly like the real file system. The whole test suite runs against both implementations, so the mock and reality stay in sync. - An
ITimeSystemabstraction forDateTime,Stopwatch,Task.Delay,Thread.Sleep,PeriodicTimerandTimer. - An
IRandomSystemabstraction forRandom(with a thread-safeSharedinstance also under .NET Framework) andGuid. - Companion packages
Testably.Abstractions.Compression(Zip files) andTestably.Abstractions.AccessControl(ACLs).
Why another file system mock?
Testably.Abstractions shares the IFileSystem interface with TestableIO.System.IO.Abstractions - your production code does not change when you switch testing libraries. What you get on top:
- Advanced testing scenarios:
FileSystemWatcher,SafeFileHandle, multiple drives with size limits. - Cross-platform simulation: run a Linux file system on Windows (and vice versa) to exercise platform-specific behaviour from a single CI job.
- A second abstraction layer for time and randomness.
See Migration from TestableIO if you are already using TestableIO.
A short example
public class MyService(IFileSystem fileSystem)
{
private readonly IFileSystem _fileSystem = fileSystem;
public void StoreData()
{
var content = GetFileContent();
_fileSystem.File.WriteAllText("result.xml", content);
}
}
[Test]
public void StoreData_ShouldWriteValidFile()
{
IFileSystem fileSystem = new MockFileSystem();
var sut = new MyService(fileSystem);
sut.StoreData();
var content = fileSystem.File.ReadAllText("result.xml");
// assert on content
}
Continue with Getting Started to install the packages and wire them up, then explore File system, Time system, Random system and the Companion libraries.