I‘m working on making a legacy code base more testable and made good progress with (constructor)-injecting dependencies to classes.
I noticed something in all classes that access the file system: they make calls to C#'s static helper classes like System.IO.File
or System.IO.Directory
. To really be able to unit test these classes I need to mock the calls to those methods as well.
Is this a common practice? I would write an interface like IFileAbstraction
which could e.g. include the method definition void WriteAllText(string path, string content)
which would then be implemented by a Mock object during unit-testing. During normal execution, I imagine a class that just routes the calls to the corresponding helper class (in this case System.IO.File#WriteAllText(string path, string content)
.
Is there a better way to achieve testability of file-system accessing classes?
Small note: I use Autofac
as a DI container, so it is pretty easy to handle mutiple injected dependencies in my refactored classes.