It is common to use Directory.GetFiles from the System.IO. But it’s a bad habbit! We should be able to Unit Test our methods. Here is a simple way to do it. In this article I will use MOQ, if you don’t know it, please check this post about Unit Testing with MOQ.

Instead of using direclty the static method Directory.GetFiles, we should create our own implementation. No worries, we will actually use Directory.GetFiles, but with our implementation, we will be able to mock files in our Unit Tests. If you don’t know Moq please have a look here.

Let’s start with the interface:

And now the implementation of IDirectoryHelper:

Now in our ConsoleApp, we will test it to be sure we get the local files as they are on the disk:

As a result, you should see the local files on your disk under C:\.

But now, we can do a Unit Test where we want to simulate some files:

Happy coding! 🙂