I’ve posted an update to the Microsoft.Activities.Simulation library. Today at the team meeting I showed the library to the Windows Workflow team and Ed Pinto said “That is cool!” so I must be doing something right.
Today’s update is a better TestConsole. This time I’ve had the TestConsole redirect the Console output stream by calling Console.SetOut. Now the stream will go into a StringWriter backed by a StringBuilder as the buffer.
This way anything that writes to the console will now be captured and therefore it will become testable.
When I want to verify that something was written to the console there are two ways I can do it. I can access the StringBuilder by calling TestConsole.Buffer.ToString() and then look for something in the string.
Or if I prefer a line oriented approach I can do something like this test.
Here I have a SimulationResults type with a TestConsole member named Console. I can access a string named Lines that will return each line from the TestConsole buffer by calling Regex.Split
Since splitting the buffer is a little work, I only split it if the length of the buffer has changed since the last time I split it. One side effect of using Regex.Split is that you always get an empty last line. Since the CancellationScope Activity Sample also uses this library I put the library in place and found that I had to update all the unit tests that relied on a line count. If they asserted the count should be 2 I had to change it to 3.
I’ m not enough of a Regex guru to know how to work around this. For now I think it is ok to just say that you will always get one empty line at the end of the array and that is just the way it is.