I would like to test a class that is using the method File.OpenRead() to obtain the content of a file. After reading the content of the file then it processes them. I have create an interface and a class that wraps the static OpenRead() method. But I encounter the problem that OpenRead() returns a FileStream and I have no idea how to "mock" the file stream.
Currently, I am creating a file just to create a FileStream... Of course, the tests are regularly failing with a IOException because the file is still in use...
Stripped Example: Class:
class FileProcessor
{
public FileProcessor(IFileWrap fileWrap) // fileWrap only redirects the calls to the static methods of File class
{ ... }
public void Process(string file)
{
var content = fileWrap.ReadAllLines(file);
// process content
}
}
And the test:
[TestClass]
public class FileProcessor_Test
{
[TestMethod]
Process_FileNotReadable_Exception()
{
File.WriteAllText(testFile, "something");
var fileWrapMock = new Mock<IFileWrap>();
FileProcessor dut = new FileProcessor(fileWrapMock.Object);
var actualException = AssertException.Throws<Exception>(() => dut.Process(testFile));
}
}
I would like to avoid creating an abstraction of FileStream, too.
I was hoping that I could create a MemoryStream and somehow use this as the input, but this would require to change the file wrapper and deviate from the actual File class.
Any input is appreciated :)
Edit:
The processing includes computing a MD5 checksum by calling ComputeHash() from the the class MD5.