1

I am trying to unit test a method that calls StreamWriter. I am trying to use System.IO.Abstraction in order to mock StreamWriter however I can't find the interface on the last NuGet looked into the source code as well but have no idea what is the replacement for this, other stuff like FileInfo is working as expected.

Thanks,

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Adel Khalil
  • 11
  • 1
  • 2
  • 1
    1. Where did you see anything that says System.IO.Abstractions has an IStreamWriter interface? It doesn't appear to. 2. Why would you need an abstraction around StreamWriter, anyway? StreamWriter is already an abstraction -- it can work with any stream you give it. Just make your tests give it a MemoryStream instead of a FileStream. – Joe White Sep 15 '14 at 11:37
  • usefull is this link: http://stackoverflow.com/questions/12480563/c-sharp-unit-test-a-streamwriter-parameter Or this could be usefull as well: http://stackoverflow.com/questions/1087351/how-do-you-mock-out-the-file-system-in-c-sharp-for-unit-testing – Daniel Dušek Sep 17 '14 at 07:17
  • 3
    @JoeWhite probably from here. http://blog.jonathanchannon.com/2012/09/28/abstracting-the-file-system/ – RubberDuck Jan 06 '16 at 19:53

2 Answers2

2

I was also looking for how to mock a FileStream via System.IO.Abstractions and couldn't see it initially. It's Hanging off the FileInfo Object. It results in slightly clunky code and required a cast. My Original Code:

FileStream fileStreamBack = null;
using (fileStreamBack = new FileStream(fileFrom, FileMode.Open, FileAccess.Read))
using (var fileStreamf = new FileStream(fileTo, FileMode.Create, FileAccess.Write))
{
                fileStreamBack.CopyTo(fileStreamf);             // Use the .Net 
                fileStreamBack.Flush(); // Making sure
                fileStreamBack.Close(); // Making sure
}

Now Replaced with

FileStream fileStreamBack = null;
using (fileStreamBack = (FileStream)_fileSystem.FileInfo.FromFileName(fileFrom).Open(FileMode.Open, FileAccess.Read))
using (var fileStreamf = (FileStream)_fileSystem.FileInfo.FromFileName(fileTo).Open(FileMode.Create, FileAccess.Write))
        {
            fileStreamBack.CopyTo(fileStreamf);             // Use the .Net 
            fileStreamBack.Flush(); // Making sure
            fileStreamBack.Close(); // Making sure
        }

There is also a MockFileStream object in the System.IO.Abstractions.TestingHelpers (for our convenience!)

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Barry McDermid
  • 319
  • 3
  • 10
2

Taking @BarryMcDermid's answer and altering it slightly you can do something along the lines of:

using (Stream fs = _fileSystem.FileStream.Create(filePath, FileMode.Create))
{
    ms.CopyTo(fs);
    fs.Flush();
    fs.Close(); 
}

By declaring the 'FileStream' as a Stream instead of a FileStream you'll then be able to use System.IO.Abstraction.TestingHelpers to test this code without getting exceptions.

There's a more fully worked example of that in my question here.

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
tomRedox
  • 28,092
  • 24
  • 117
  • 154