When unit testing, it is always hard to know how low down to the framework you go.
If we have a class that directly depends on the .NET Framework, i.e. the System.IO.File class, then we cant really test that in isolation.
Sure we could wrap it and inject it into the dependent class, but then we start to wrap every .NET class! What if that wrapper is not a straight call through? Perhaps it does some checking/business logic first, that you would want to test?
Currently we wrap down to a certain level, and then just don't bother unit testing that wrapper. Perhaps this is ok, because it will be tested later on through integration and exploratory testing?
Here is an example in C# just to illustrate my point:
This class is tightly coupled to the .NET Framework.. thats fine, but now I cant test it in isolation, it requires files to exist etc.
public class PathResolver
{
public string Resolve(string filename)
{
string completePath = string.Empty;
if(!File.Exists(filename))
{
return Path.Combine(@"D:\MyExample", filename);
}
return completePath;
}
}
We can unit test this by doing something like:
public class PathResolver
{
private readonly IFileSystem _fileSystem;
public PathResolver(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
public string Resolve(string filename)
{
string completePath = string.Empty;
if(!_fileSystem.Exists(filename))
{
return _fileSystem.Combine(@"D:\MyExample", filename);
}
return completePath;
}
}
But now we cant test the "FileSystem" class!
What are other peoples thoughts?