Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 18 | Comments: 66

filter by tags archive

Mocking the file system

time to read 1 min | 94 words

I have the following piece of code:

image

And I wasn't sure about how I can test this thing. For production, this will use a File Stream, and getting those to produce errors is non trivial in most situations. A bit of thinking, however, told me that I can write a test for the error handling section of this fairly easily, like this:

 

image

I love having powerful tools at my reach.


Comments

Matt Campbell

At work, we have been working around the issue of testing things that interact with the filesystem. Frustrated by the extreme lack of abstraction in the Framework's System.IO namespace, we decided to create our own abstraction layer called ISystemIOAdapter (which is really a cross between an Adapter pattern and a Proxy pattern, but I digress).

The implementation of SystemIOAdapter is a trivial pass-through to the System.IO methods (many of them statics, such as Directory.GetFiles). In our case, since we're not using an IoC container, we also had to make it a Singleton with a leaky encapsulation (we needed a ResetInstance() method in order to clean up after tests where we mocked it).

This has allowed us to use Rhino Mocks to create clean interaction-based tests of things which I.E. need to iterate through a directory structure in a specific way, which do not require complicated/expensive setups of creating actual directories and files. It's been a real help to us, since our current projects are doing a whole lot of filesystem work.

Ayende Rahien

Matt,

I think that Bill Simser has something like that on CodePlex as well. This approach is very common.

In this case, I wanted something very specific, which was why I used this approach instead of full blown adapter.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. RavenDB 3.0 New Stable Release - about one hour from now
  2. Production postmortem: The case of the lying configuration file - about one day from now
  3. Production postmortem: The industry at large - 2 days from now
  4. The insidious cost of allocations - 3 days from now
  5. Buffer allocation strategies: A possible solution - 6 days from now

And 4 more posts are pending...

There are posts all the way to Sep 11, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    31 Aug 2015 - The case of the memory eater and high load
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats