If you are working with .Net and you need to read/write files you may be familiar with the System.IO.File class which provides many methods to manipulate files. But there is an issue with that class: its methods are static and because of that you won’t be able to mock it*.

After dealing with this situation for several years and using always a very similar strategy, I finally decided to create a reusable component to encapsulate the System.IO.File class.

The component I created is called System.IO.Utilities, its code is available on Github and it is also published on NuGet, ready for you to use it. At this moment it just provides an interface with one implementation that wraps the System.IO.File class.

Here you can see how to use it.

Contributions are welcome 😉

(*to be more precise: you won’t be able to mock that class with proxy-based mocking tool like Moq, but you can still mock it with «lower-level» tool like TypeMock)

2 respuestas a «No more System.IO.File»

  1. Hola. Justo estaba queriendo mockear el File System para un proyecto personal y me topé con este articulo que me pareció interesante. Después descubrí que existe el paquete System.IO.Abstractions: https://www.nuget.org/package/System.IO.Abstractions

    Saludos,

    1. Gracias Matias por compartir.
      Creo que en el momento en que escribí el artículo, System.IO.Abstractions no estaba disponible para .netCore (pero no estoy seguro).

      Ojo que el link que compartiste no es correcto, el link correcto es este: https://www.nuget.org/packages/System.IO.Abstractions

      Saludos!

Deja un comentario

Este sitio utiliza Akismet para reducir el spam. Conoce cómo se procesan los datos de tus comentarios.