Updated version of question
Hi.
My company has a few legacy code bases which I hope to get under test as soon as they migrate to .NET 3.5. I have selected Moq as my Mocking framework (I fell in love with the crisp syntax immediately).
One common scenario, which I expect to see a lot of in the future, is where I see an object which interacts with some other objects.
I know the works of Michael Feathers and I am getting good at identifying inflection points and isolating decent sized components. Extract and Override is king.
However, there is one feature which would make my life a whole lot easier.
Imagine Component1 interacting with Component2. Component2 is some weird serial line interface to a fire central or somesuch with a lot of byte inspection, casting and pointer manipulation. I do not wish to understand component2 and its legacy interface consumed by Component1 carries with it a lot of baggage.
What I would like to do, is to extract the interface of Component2 consumed by Component1 and then do something like this:
component1.FireCentral = new Mock<IComponent2> (component2);
I am creating a normal mock, but I am pasing in an instance of the real Component2 in as a constructor argument into the Mock object. It may seem like I'm making my test depending on Component2, but I am not planning on keeping this code. This is part of the "place object under test" ritual.
Now, I would fire up the real system (with a physical fire central connected) and then interact with my object.
What I then would wish for, is to inspect the mock to see a log of how component1 interacted with component2 (using the debugger to inspect some collection of strings on the mock). And, even better, the mock could provide a list of expectations (in C#) that would create this behavior in a mock that did not depend on Component2, which I would then use in my test code.
In short. Using the mocking framework to record the interaction so that I can play it back in my test code.
Old version of question
Hi.
Working with legacy code and with a lot of utility classes, I sometimes find myself wondering how a particular class is acted upon by its surroundings in a number of scenarios. One case that I was working on this morning involved subclassing a regular MemoryStream so that it would dump its contents to file when reaching a certain size.
// TODO: Remove
private class MyLimitedMemoryStream : MemoryStream
{
public override void Write(byte[] buffer, int offset, int count)
{
if (GetBuffer().Length > 10000000)
{
System.IO.FileStream file = new FileStream("c:\\foobar.html",FileMode.Create);
var internalbuffer = GetBuffer();
file.Write(internalbuffer,0,internalbuffer.Length);
}
base.Write(buffer, offset, count);
}
}
(and I used a breakpoint in here to exit the program after the file was written). This worked, and I found which webform (web part->web part->web part) control rendered incorrectly. However, memorystream has a bunch of write's and writeline's.
Can I use a mocking framework to quickly get an overview on how a particular instance is acted upon? Any clever tricks there? We use Rhino Mocks.
I see this as a great assett in working with legacy code. Especially if recorded actions in a scenario easily can be set up as new expectations/acceptance criteria for that same scenario replicated in a unit test.
Every input is appreciated. Thank you for reading.