Needed: File system interfaces and implementation in .NET

.net, dependency-injection, file-io, mocking, unit-testing

Solution

I've written adapters for the static `System.IO.File` and `Directory` methods. Then in my classes I do the following:

public class MyService {
  public IFile File {private get;set;}
  public MyService() {
    File = new FileImpl();
  }
  public void DoSomething() {
    File.ReadAllText("somefile");
  }
}

Then you can inject a mock as IFile for testing.

Problem

Possible Duplicate: How do you mock out the file system in C# for unit testing? I write unit tests to my code, using Moq as a mocking framework. My code includes calls to the file system, using direct calls to `System.IO` classes. For instance, `File.Exists(...)` etc. I'd like to change that code to be more testable, so I should have an interface, say `IFile`, with a relevant method, say `Exists(string path)`. I know I can write it from scratch, but I thought that maybe there's a complete, robust framework that has both interfaces and implementations for the file system. This (desired) framework may also be some kind of a "service", and therefore its API doesn't have to be an "interface equivalent" to the `System.IO` namespace. Note that I'd really like to have interfaces (and not static methods) so that my code is ready for dependency injection What I got so far: - A somehow similar, yet not the same question was asked in stackoverflow - In codeplex.com there's a project named CodePlex Source Control Client (link) that has such classes in the source code (see `/Source/TfsLibrary/Utility/` in the source code for specific details) Any other recommendations?

Original source

Related problems