System.IO.Abstractions.TestingHelpers 14.0.3

Install-Package System.IO.Abstractions.TestingHelpers -Version 14.0.3
dotnet add package System.IO.Abstractions.TestingHelpers --version 14.0.3
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="14.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.IO.Abstractions.TestingHelpers --version 14.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: System.IO.Abstractions.TestingHelpers, 14.0.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install System.IO.Abstractions.TestingHelpers as a Cake Addin
#addin nuget:?package=System.IO.Abstractions.TestingHelpers&version=14.0.3

// Install System.IO.Abstractions.TestingHelpers as a Cake Tool
#tool nuget:?package=System.IO.Abstractions.TestingHelpers&version=14.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

System.IO.Abstractions NuGet Continuous Integration Codacy Badge Renovate enabled FOSSA Status

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.

dotnet add package System.IO.Abstractions
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 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.

dotnet add package System.IO.Abstractions.TestingHelpers
[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
        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 casting from the .NET Framework's untestable types to our testable wrappers:

FileInfo SomeApiMethodThatReturnsFileInfo()
{
    return new FileInfo("a");
}

void MyFancyMethod()
{
    var testableFileInfo = (FileInfoBase)SomeApiMethodThatReturnsFileInfo();
    ...
}

Since version 4.0 the top-level APIs expose interfaces instead of abstract base classes (these still exist, though), allowing you to completely mock the file system. Here's a small example, using 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:\Some\Directory", "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);
        }
    }
}

NuGet packages (6)

Showing the top 5 NuGet packages that depend on System.IO.Abstractions.TestingHelpers:

Package Downloads
Glob.cs

File system path globbing library (wildcards like in bash).

Reo.Core.Testing

Package Description

Noggog.Testing

Package Description

CreativeCoders.UnitTests

Package Description

NEALib

https://github.com/CopenhagenCityArchives/nealib

GitHub repositories (31)

Showing the top 5 popular GitHub repositories that depend on System.IO.Abstractions.TestingHelpers:

Repository Stars
microsoft/PowerToys
Windows system utilities to maximize productivity
gitextensions/gitextensions
Git Extensions is a standalone UI tool for managing git repositories. It also integrates with Windows Explorer and Microsoft Visual Studio (2015/2017/2019).
JosefNemec/Playnite
Video game library manager with support for wide range of 3rd party libraries and game emulation support, providing one unified interface for your games.
projectkudu/kudu
Kudu is the engine behind git/hg deployments, WebJobs, and various other features in Azure Web Sites. It can also run outside of Azure.
Lidarr/Lidarr
Looks and smells like Sonarr but made for music.
Version Downloads Last updated
14.0.3 294 11/27/2021
14.0.2 169 11/26/2021
14.0.1 176 11/26/2021
13.2.47 102,822 8/25/2021
13.2.46 154 8/25/2021
13.2.45 147 8/25/2021
13.2.43 54,028 7/27/2021
13.2.42 12,530 7/23/2021
13.2.41 12,633 7/15/2021
13.2.40 1,014 7/14/2021
13.2.39 491 7/14/2021
13.2.38 44,124 6/15/2021
13.2.37 1,968 6/10/2021
13.2.36 199 6/10/2021
13.2.35 158 6/10/2021
13.2.34 175 6/10/2021
13.2.33 53,538 5/15/2021
13.2.32 203 5/15/2021
13.2.31 26,016 5/2/2021
13.2.30 300 5/2/2021
13.2.29 49,018 4/14/2021
13.2.28 39,051 3/25/2021
13.2.27 1,060 3/25/2021
13.2.25 50,276 3/9/2021
13.2.24 3,343 3/6/2021
13.2.23 31,900 2/24/2021
13.2.22 296 2/24/2021
13.2.20 1,397 2/23/2021
13.2.19 194 2/23/2021
13.2.18 348 2/23/2021
13.2.17 1,268 2/23/2021
13.2.16 290 2/22/2021
13.2.15 9,197 2/18/2021
13.2.14 240 2/18/2021
13.2.13 244 2/18/2021
13.2.12 197 2/18/2021
13.2.11 3,256 2/16/2021
13.2.10 44,333 2/10/2021
13.2.9 155,806 1/14/2021
13.2.8 35,035 12/22/2020
13.2.7 6,359 12/20/2020
13.2.6 4,825 12/16/2020
13.2.5 31,438 12/9/2020
13.2.4 8,255 12/5/2020
13.2.3 490 12/4/2020
13.2.2 30,630 11/26/2020
13.2.1 1,628 11/26/2020
13.1.2 337 11/25/2020
13.1.1 348 11/25/2020
13.0.1 19,184 11/21/2020
12.2.27 9,993 11/21/2020
12.2.26 1,152 11/20/2020
12.2.25 4,764 11/17/2020
12.2.24 5,773 11/15/2020
12.2.23 357 11/15/2020
12.2.22 418 11/14/2020
12.2.21 3,628 11/12/2020
12.2.20 294 11/12/2020
12.2.19 18,736 11/5/2020
12.2.7 35,252 10/15/2020
12.2.6 7,011 10/15/2020
12.2.5 3,363 10/12/2020
12.2.4 321 10/12/2020
12.2.3 38,598 10/12/2020
12.2.2 5,222 10/7/2020
12.2.1 52,209 9/28/2020
12.1.11 1,004 9/28/2020
12.1.10 4,063 9/24/2020
12.1.9 21,397 9/11/2020
12.1.2 324 9/11/2020
12.1.1 146,079 8/3/2020
12.0.13 2,982 8/2/2020
12.0.10 5,692 7/25/2020
12.0.9 9,756 7/21/2020
12.0.5 29,926 7/11/2020
12.0.4 20,534 7/2/2020
12.0.3 5,395 6/29/2020
12.0.2 46,089 6/23/2020
12.0.1 13,770 6/20/2020
11.0.18 5,156 6/20/2020
11.0.17 2,636 6/19/2020
11.0.16 440 6/18/2020
11.0.15 1,456 6/18/2020
11.0.14 611 6/17/2020
11.0.13 481 6/17/2020
11.0.12 383 6/17/2020
11.0.7 41,499 5/28/2020
11.0.6 80,303 5/8/2020
11.0.5 9,241 5/6/2020
11.0.4 10,039 5/1/2020
11.0.3 1,142 4/30/2020
11.0.2 47,294 4/26/2020
11.0.1 375 4/26/2020
10.0.10 8,269 4/20/2020
10.0.9 9,823 4/17/2020
10.0.8 25,110 4/7/2020
10.0.7 4,639 4/3/2020
10.0.6 10,444 4/1/2020
10.0.5 399 4/1/2020
10.0.4 395 4/1/2020
10.0.1 38,538 3/21/2020
9.0.6 4,359 3/19/2020
9.0.5 22,748 3/16/2020
9.0.4 121,704 2/18/2020
9.0.3 442 2/18/2020
9.0.2 7,113 2/11/2020
9.0.1 463 2/11/2020
8.1.1 629 2/11/2020
8.0.6 466 2/11/2020
8.0.5 25,271 1/29/2020
8.0.4 1,000 1/27/2020
8.0.3 7,749 1/19/2020
7.1.10 30,556 1/17/2020
7.1.8 426 1/17/2020
7.1.4 15,779 1/13/2020
7.1.3 21,065 1/6/2020
7.1.1 19,887 12/21/2019
7.0.16 2,087 12/19/2019
7.0.15 33,567 12/5/2019
7.0.7 99,293 10/21/2019
7.0.5 12,033 10/11/2019
7.0.4 91,067 9/29/2019
6.0.38 46,842 9/26/2019
6.0.36 1,781 9/24/2019
6.0.34 862 9/24/2019
6.0.32 27,227 9/9/2019
6.0.27 12,679 9/3/2019
6.0.25 509 9/2/2019
6.0.23 14,033 8/26/2019
6.0.21 42,957 8/12/2019
6.0.19 40,894 8/9/2019
6.0.17 6,964 8/5/2019
6.0.15 52,737 7/9/2019
6.0.14 16,974 6/29/2019
6.0.13 573 6/28/2019
6.0.11 13,969 6/21/2019
6.0.9 466 6/21/2019
6.0.7 11,991 6/16/2019
6.0.6 429 6/16/2019
6.0.5 1,740 6/13/2019
6.0.3 9,678 6/13/2019
6.0.1 37,251 6/7/2019
5.0.1 8,264 6/3/2019
4.2.17 1,765 5/30/2019
4.2.15 4,734 5/28/2019
4.2.13 24,523 5/15/2019
4.2.12 487 5/15/2019
4.2.10 18,704 5/10/2019
4.2.9 605 5/10/2019
4.2.8 22,795 4/28/2019
4.2.4 7,819 4/19/2019
4.1.6 61,767 4/9/2019
4.0.11 51,934 3/30/2019
3.1.1 29,435 3/10/2019
3.0.10 213,909 1/5/2019
3.0.2 51,269 12/7/2018
2.2.18-beta 494 12/3/2018
2.2.17-beta 468 12/2/2018
2.2.16-beta 444 12/1/2018
2.2.15-beta 456 12/1/2018
2.2.14-beta 450 12/1/2018
2.2.13-beta 495 12/1/2018
2.2.12-beta 465 12/1/2018
2.2.11-beta 457 12/1/2018
2.2.10-beta 471 11/28/2018
2.2.9-beta 564 11/16/2018
2.2.8-beta 537 11/9/2018
2.2.7-beta 513 11/5/2018
2.2.6-beta 520 10/30/2018
2.2.5-beta 484 10/30/2018
2.2.4-beta 502 10/30/2018
2.2.3-beta 494 10/29/2018
2.2.2-beta 494 10/25/2018
2.1.0.256 91,952 10/20/2018
2.1.0.247 26,972 10/15/2018
2.1.0.237 2,498 10/14/2018
2.1.0.236 49,091 10/10/2018
2.1.0.235 17,225 10/8/2018
2.1.0.234 655 10/8/2018
2.1.0.233 1,840 10/6/2018
2.1.0.232 1,591 10/4/2018
2.1.0.231 18,417 9/19/2018
2.1.0.230 24,262 9/8/2018
2.1.0.229 1,620 9/6/2018
2.1.0.228 8,619 8/27/2018
2.1.0.227 38,175 8/16/2018
2.1.0.226 9,165 8/14/2018
2.1.0.217 1,980 8/10/2018
2.1.0.216 4,509 8/9/2018
2.1.0.215 3,384 8/6/2018
2.1.0.214 986 8/5/2018
2.1.0.213 755 8/4/2018
2.1.0.211 25,509 7/25/2018
2.1.0.210 3,312 7/21/2018
2.1.0.209 2,835 7/20/2018
2.1.0.208 2,964 7/17/2018
2.1.0.207 776 7/17/2018
2.1.0.206 9,544 7/15/2018
2.1.0.205 994 7/12/2018
2.1.0.204 891 7/11/2018
2.1.0.203 804 7/11/2018
2.1.0.202 942 7/10/2018
2.1.0.201 1,344 7/10/2018
2.1.0.200 1,040 7/9/2018
2.1.0.199 794 7/9/2018
2.1.0.198 1,566 7/8/2018
2.1.0.197 792 7/8/2018
2.1.0.196 811 7/8/2018
2.1.0.195 799 7/7/2018
2.1.0.194 777 7/7/2018
2.1.0.193 793 7/7/2018
2.1.0.192 782 7/7/2018
2.1.0.191 741 7/7/2018
2.1.0.190 787 7/7/2018
2.1.0.189 803 7/7/2018
2.1.0.188 726 7/6/2018
2.1.0.187 6,685 7/6/2018
2.1.0.186 9,838 7/6/2018
2.1.0.185 795 7/5/2018
2.1.0.184 8,085 7/4/2018
2.1.0.183 836 7/4/2018
2.1.0.182 785 7/4/2018
2.1.0.181 778 7/4/2018
2.1.0.180 756 7/4/2018
2.1.0.179 792 7/4/2018
2.1.0.178 188,031 1/11/2018
2.1.0.177 4,731 1/2/2018
2.1.0.176 9,081 12/8/2017
2.1.0.175 23,920 11/16/2017
2.1.0.174 22,927 11/7/2017
2.1.0.173 755 11/7/2017
2.1.0.172 723 11/7/2017
2.1.0.171 1,279 11/4/2017
2.1.0.170 738 11/4/2017
2.1.0.169 752 11/4/2017
2.1.0.168 735 11/4/2017
2.1.0.166 739 11/4/2017
2.1.0.164 736 11/4/2017
2.1.0.163 715 11/4/2017
2.1.0.159 31,232 10/22/2017
2.0.0.143 419,838 4/7/2017
2.0.0.142 826 4/7/2017
2.0.0.141 8,213 3/2/2017
2.0.0.140 11,232 1/17/2017
2.0.0.139 7,157 1/6/2017
2.0.0.138 15,088 11/17/2016
2.0.0.137 4,645 10/14/2016
2.0.0.136 2,002 10/1/2016
2.0.0.124 133,070 2/8/2016
2.0.0.123 6,868 12/29/2015
2.0.0.122 853 12/28/2015
2.0.0.121 830 12/28/2015
2.0.0.120 6,293 12/6/2015
2.0.0.119 851 12/6/2015
2.0.0.118 2,863 11/4/2015
2.0.0.117 1,787 10/19/2015
2.0.0.116 7,759 7/20/2015
2.0.0.115 7,959 5/18/2015
2.0.0.114 963 5/18/2015
2.0.0.113 5,844 3/18/2015
2.0.0.112 1,214 3/11/2015
2.0.0.111 851 3/11/2015
2.0.0.110 877 3/11/2015
2.0.0.109 852 3/11/2015
2.0.0.108 1,164 3/4/2015
2.0.0.107 1,373 2/23/2015
2.0.0.106 1,267 2/20/2015
2.0.0.105 876 2/19/2015
2.0.0.104 1,159 2/14/2015
2.0.0.103 1,354 2/7/2015
2.0.0.102 882 2/7/2015
2.0.0.101 1,278 1/25/2015
2.0.0.100 907 1/25/2015
2.0.0.99 901 1/25/2015
2.0.0.98 922 1/25/2015
1.4.0.93 6,270 1/25/2015
1.4.0.92 12,005 9/29/2014
1.4.0.89 877 9/29/2014
1.4.0.88 889 9/29/2014
1.4.0.87 1,256 9/21/2014
1.4.0.86 6,271 5/7/2014
1.4.0.85 1,884 4/23/2014
1.4.0.84 1,191 4/7/2014
1.4.0.83 1,442 3/24/2014
1.4.0.82 1,003 3/24/2014
1.4.0.81 3,640 3/17/2014
1.4.0.80 963 3/17/2014
1.4.0.79 4,467 3/10/2014
1.4.0.78 1,065 3/2/2014
1.4.0.77 959 3/2/2014
1.4.0.76 1,164 2/21/2014
1.4.0.75 999 2/20/2014
1.4.0.74 21,345 1/12/2014
1.4.0.73 1,058 12/22/2013
1.4.0.72 1,361 12/1/2013
1.4.0.71 945 12/1/2013
1.4.0.70 1,170 11/21/2013
1.4.0.69 1,061 11/20/2013
1.4.0.68 1,664 10/15/2013
1.4.0.67 922 10/15/2013
1.4.0.66 2,384 7/31/2013
1.4.0.65 1,510 7/9/2013
1.4.0.64 1,810 4/26/2013
1.4.0.63 1,055 4/26/2013
1.4.0.62 1,001 4/26/2013
1.4.0.61 1,019 4/25/2013
1.4.0.60 961 4/25/2013
1.4.0.59 972 4/25/2013
1.4.0.58 970 4/25/2013
1.4.0.57 997 4/25/2013
1.4.0.56 1,008 4/25/2013
1.4.0.55 1,011 4/25/2013
1.4.0.54 1,058 4/25/2013
1.4.0.53 982 4/25/2013
1.4.0.52 1,068 4/25/2013
1.4.0.51 1,010 4/22/2013
1.4.0.50 986 4/22/2013
1.4.0.49 1,196 4/11/2013
1.4.0.48 1,075 3/24/2013
1.4.0.47 1,052 3/24/2013
1.4.0.46 978 3/24/2013
1.4.0.45 990 3/24/2013
1.4.0.44 1,422 3/16/2013
1.4.0.43 970 3/16/2013
1.4.0.42 1,042 3/16/2013
1.4.0.41 1,045 3/6/2013
1.4.0.40 1,560 12/24/2012
1.4.0.39 1,026 12/23/2012
1.4.0.38 990 12/23/2012
1.4.0.37 1,906 11/29/2012
1.4.0.36 1,087 11/29/2012
1.4.0.35 1,401 9/25/2012
1.4.0.32 2,264 7/14/2012
1.4.0.31 1,026 7/14/2012
1.4.0.30 1,047 7/12/2012
1.4.0.29 1,012 7/12/2012
1.4.0.28 997 7/12/2012
1.4.0.27 1,003 7/12/2012
1.4.0.26 1,093 7/2/2012
1.4.0.25 1,040 7/2/2012
1.4.0.24 1,554 5/15/2012
1.4.0.23 1,416 4/25/2012
1.4.0.22 1,056 4/25/2012
1.4.0.21 1,001 4/25/2012
1.4.0.20 1,150 4/18/2012
1.4.0.19 1,056 4/18/2012
1.4.0.18 1,049 4/18/2012
1.4.0.17 1,062 4/18/2012
1.4.0.14 1,085 4/4/2012
1.4.0.13 1,354 3/27/2012
1.4.0.12 1,649 9/16/2011
1.4.0.11 1,217 9/16/2011
1.3.0 1,419 5/27/2011
1.2.0 1,739 5/26/2011