6

I'm writing some unit tests using System.IO.Abstractions following the sample code in GitHub and got stuck here:

fileSystem: new FileSystem() //use default implementation which calls System.IO

This line is meant to initialise System.IO.FileSystem but that class does not exist... do you know if it has been deprecated or am I getting this wrong?

Thanks!

Gaby
  • 61
  • 1
  • 1
  • 3

1 Answers1

11

yes, in the production code, you should inject an instance of System.IO.Abstractions.FileSystem. In your test code an instance of System.IO.Abstractions.TestingHelpers.MockFileSystem.

manne
  • 194
  • 2
  • 5
  • 2
    Clarification for future readers: Since they are abstractions, the type you insert shouldn't matter. In other words, you can certainly insert an instance of the ''regular' file system. But your production code should certainly accept classes within the System.IO.Abstractions.FileSystem dll as parameters, rather than the 'regular' file system classes. – BenKoshy Aug 16 '18 at 03:21
  • But to be clear, we have to literally change the imported namespaces in our production code just to allow for unit testing... seems anti-pattern to me – fullStackChris Mar 16 '22 at 07:58
  • @fullStackChris If you have System.IO usings, perhaps that is the anti-pattern. That is hard-coding against a specific filesystem IO implementation, rather than depending on an abstraction. By making the change to System.IO.Abstractions, you are actually fixing that. – mlibby Apr 07 '23 at 18:23