I have a business level class that uses System.IO.File to read information from different files. For the unit test of this class, I chose to replace the dependency on the File class with a nested dependency like this:
using System.IO; public interface IFileWrapper { bool FileExists( string pathToFile ); Stream Open( string pathToFile ); }
Now I can test my class using Mock, and everything is fine with the world. Separately, I need a specific implementation. I have the following:
using System; using System.IO; public class FileWrapper : IFileWrapper { public bool FileExists( string pathToFile ) { return File.Exists( pathToFile ); } public Stream Open( string pathToFile ) { return File.Open( pathToFile, FileMode.Open, FileAccess.Read, FileShare.Read ); } }
Now my business class is no longer dependent on the System.IO.File class and can be tested using the Mock from IFileWrapper. I do not see the need to test the System.IO.File class, as I assume that this has been thoroughly tested by Microsoft and proven for countless purposes.
How to check a specific FileWrapper class? Although this is a simple class (low risk), I have larger examples that follow the same approach. I can't get close to 100% code coverage (assuming it's important) without doing this.
The broader question here, I believe, is how to bridge the gap between unit testing and integration testing? Do I need to test this class or is there some kind of attribute to decorate this class in order to exclude this from the code coverage calculation.
Kerry carroll
source share