Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking a Using with a FileStream

Tags:

c#

rhino-mocks

I have been trying to mock out a using with a file stream but have not been able to complete this and am unsure how to do it, I am using rhino mock.

private Connection LoadConnectionDetailsFromDisk(string bodyFile)
{     
   //logic before
   using (FileStream fs = File.Open(bodyFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
   {
     return this.serverConfiguration.LoadConfiguration(fs, flowProcess);
   }
    //more logic
}

Could anyone tell me how to mock the using(FileStream....) so I am able to access this branch of the code?

like image 451
user101010101 Avatar asked Jun 21 '12 15:06

user101010101


People also ask

How do you mock a StreamReader?

What you can do instead is create an interface, let's call it IFileManager for arguments sake, with a method called StreamReader . Now you can mock the IFileManager interface and its StreamReader method, you can inject this mocked instance into the Foo class making it available to the ParseFile method to use.

What is the use of FileStream?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.

How do I delete a FileStream?

You cannot delete the file because you did not close the FileStream. What you can do though is copy the contents in a MemoryStream, close the FileStream and then delete the File.


1 Answers

You have to abstract File.Open() by an interface method then you would be able mocking call to it.

So

1) Create an interface:

public interface IFileDataSource
{
   FileStream Open(string path,
                   FileMode mode,
                   FileAccess access,
                   FileShare share);
}

2) Change LoadConnectionDetailsFromDisk() as following:

private Connection LoadConnectionDetailsFromDisk(string path, IFileDataSource fileSource)
{     
   using (FileStream fs = fileSource.Open(bodyFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
   {
      return this.serverConfiguration.LoadConfiguration(fs, flowProcess);
   }

   //more logic
}

3) In a test mock the interface and inject a mock

// create a mock instance
var sourceMock = MockRepository.GenerateMock<IFileDataSource>();

// setup expectation
sourceMock.Expect(m => m.Open("path", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
         .CallBack(
 delegate (string path, FileMode mode, FileAccess access, FileShare share)
 {
      // handle a call

     return true;
 }).Repeat.Any();

// TODO: depends on how you are triggering LoadConnectionDetailsFromDisk method call
// inject a mock

Considering that LoadConnectionDetailsFromDisk() you can not inject mock directly to this method call froma test so please show how this method is invoked.

like image 130
sll Avatar answered Sep 25 '22 11:09

sll