tags:

views:

288

answers:

5

For some reason I can't find one, but someone must have already created a .NET IO library wrapper. I want to be able to mock calls to File.Exists etc, and the static methods builtin don't lend well to this.

+1  A: 

There is also a FileInfo class, which does the same things as the static methods.

On the other hand, you probably don't want to mock the entirety of the FileInfo class. Instead, you want to place all of your file operations into a single class, then extract an interface from the class (describing the public methods), and use the interface to mock the file operations that you perform, not the entire set of operations that Microsoft thinks should be in the FileInfo class.

John Saunders
A: 

Instead of looking for a library that wraps the whole filesystem. Why not create a simple IFileSystem interface and start adding the methods that you need.

The rest of your application should depend on IFileSystem which will allow for mocking

You can have one implementation that simply calls the static methods that .Net gives you and then your code can depend on the interface.

ooo
This is what I've done in the past, but I get tired of continually re-creating this interface in every project I work on. Looking for a little code re-use here.
Sneal
put it in a shared library and reference it in all of your projects
ooo
+3  A: 

All good answers, but they all left me where I started - recreating some sort of IFileSystem for every project I end up working on. I ended up creating a generic IFileSystem library around the .NET libraries that I can use reuse between all my projects. Not great, but it works for now.

Sneal
+1  A: 

I found SystemWrapper a few minutes before yours, but yours suits my purposes better.

Precipitous
A: 

I maintain the Jolt.NET project on CodePlex, which contains a library to generate such interfaces and their implementations for you. Please refer to the Jolt.Testing library for more information.

Steve Guidi