Skip to main content

Posts

Showing posts from January, 2015

My New Years Resolution - Use more shims

When I need to unit test my code in isolation, I typically use Moq to mock the interfaces that aren't related to the methods I'm testing. Until today, that is. I wanted to test a little bit of functionality that depends on values coming back from the System.IO namespace in .NET 4.5. Specifically, if a file in a given folder was more than a certain number of days old, it would get deleted. How do you unit test that? Here is the code that needs to be tested.

publicvoid Rotate() {     var searchPattern = "*." + BaseName;     var files = Directory.GetFiles(Folder, searchPattern);     foreach (var file in files)     {         if (File.GetCreationTime(file) < DateTime.Now.Add(-MaxLogAge))         {             File.Delete(file);        }     } }
In the past, I probably wouldn't have bothered testing it. After all, it's pretty straightforward. Today I was feeling extra cautious.

The first problem with testing this is that Directory.GetFiles() is going to return m…