Stubbing a File Stream, Memory Stream, or Stream

Here’s the class that has a Func setup as a property. The reason this is setup, is so that the stream can actually be stubbed on the ComplianceReportingJob. Otherwise if the Stream was just being used as is, instantiated in the class, or passed in via injection, we still couldn’t get it mocked or stubbed. Especially if the class ComplianceReportingJob is under test.

[sourcecode language=”csharp”]
public class ComplianceReportingJob : IJob
{
readonly ITradeRepository tradeRepo;
readonly IFileSystem fileSys;

public ComplianceReportingJob(ITradeRepository tradeRepository, IFileSystem fileSystem)
{
tradeRepo = tradeRepository;
StreamedFile = filename => new FileStream(filename, FileMode.Create);
}

public Func<string, Stream> StreamedFile { get; set; }
}
[/sourcecode]

What we have here is the context being setup, with the property being stubbed with a fake StreamedFile = filename => new MemoryStream(). So if you’re looking for a way to test streams with .NET, this is a way around the limitations of the framework.

[sourcecode language=”csharp”]
public class with_a_compliance_report_job
{
protected static ITradeRepository tradeRepository;
protected static IFileSystem fileSys;
protected static ComplianceReportingJob job;

Establish context = () =>
{
tradeRepository = Substitute.For<ITradeRepository>();
fileSys = Substitute.For<IFileSystem>();

reportContent = FizzWare.NBuilder.Builder<TradeComplianceViewModel>.CreateListOfSize(10).Build().ToList();

tradeRepository.GetComplianceReport().Returns(reportContent);
fileSys.DirectoryExists(string.Empty).ReturnsForAnyArgs(true);

job = new ComplianceReportingJob(tradeRepository, fileSys)
{StreamedFile = filename => new MemoryStream()};
};

protected static List<TradeComplianceViewModel> reportContent;
}
[/sourcecode]

This is a pretty sweet solution, for now. The ideal solution however, would be that Microsoft fixes the framework, which is very unlikely to happen. The whole closed, sealed, and otherwise locked up framework stack is a massive problem with testability. Which then directly impacts maintainability and other things over time.

Shout it