Since the Exists method is a static method in the File class, you cannot mock it (see the note below). The easiest way to get around this is to write a thin shell around the File class. This class should implement an interface that can be injected into your class.
public interface IFileWrapper { bool Exists(string path); } public class FileWrapper : IFileWrapper { public bool Exists(string path) { return File.Exists(path); } }
Then in your class:
public class FileClass { private readonly IFileWrapper wrapper; public FileClass(IFileWrapper wrapper) { this.wrapper = wrapper; } public string GetContentFromFile(string path){ if (wrapper.Exists(path)) {
NOTE. TypeMock allows you to mock static methods. Other popular structures, for example. Moq, Rhino Mocks, etc., None.
csano
source share