This project is read-only.

Why not make FileInfoWrap.FileInfoInstance property to be virtual

Jul 27, 2015 at 12:58 PM
I have a scenario where I'd want to mock the IFileStreamWrap to return some fake Name value, but for a FileStreamInstance to return real FileStream that I've previously created. For example:

var _fileStreamMock = new Mock<IFileStreamWrap>();
_fileStreamMock.Setup(stream => stream.FileStreamInstance.Name).Returns("MT100.123.456.txt");

var filestream = new FileStream(pathToTheRealFile);
_fileStreamMock.Setup(stream => stream.FileStreamInstance).Returns(filestream);

For this code I'm getting:
System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: stream => stream.FileStreamInstance.Name

Am I wrong or if FileInfoWrap.FileInfoInstance property was set as virtual this would work?

Thanks in advance
Jul 27, 2015 at 1:01 PM
Actually I thought the problem was mocking FileStreamInstance, but it was about FileStreamInstance.Name.
I've changed the second line from code above from:

_fileStreamMock.Setup(stream => stream.FileStreamInstance.Name).Returns("MT100.123.456.txt");

_fileStreamMock.Setup(stream => stream.Name).Returns("MT100.123.456.txt");

and it was okay.