Tuesday, January 27, 2009

100% Code Coverage is hard on Legacy Code

I have some legacy code that I am working on that has a simplified contract that looks like this:

virtual void ProcessBatch(OutputBatch batch)



The class that implements this contract is loaded by reflection and this method is called based on the execution context.I had a requirement to send a message to a queue once this method completed successfully so I created the following overload:
void ProcessBatch(OutputBatch batch, DataSourceQueue queue)
What I had to do to make this work is extract the execution code from the first method and place that into my overload and then call my overload from the first method. This all works fine; however I have no way to test the fact that the first method calls the second method.To demonstrate I wrote the following test:
[Test]
public void should_call_the_overload_method_taking_the_queue()
{
device.ProcessBatch(batch);
device.AssertWasCalled(x => x.ProcessBatch (Arg<OutputBatch>.Is.TypeOf, Arg<ProviderMailboxQueue>.Is.TypeOf));
}
I must be doing something wrong here because my brain tells me this should work but instead I get the message that this should be called once but was not.

2 comments:

Sir Richard Hoare said...

How about TypeMock, Moles, or Telerik's JustMock for this?

Michael said...

Funny... This was so long ago that I forgot how this ended up. I typically use RhinoMocks which has served me well for a free tool