Verbosity Isn’t Always a Bad Thing

Posted by PSteele on ASP.net Weblogs See other posts from ASP.net Weblogs or by PSteele
Published on Fri, 21 May 2010 20:32:16 GMT Indexed on 2010/05/21 20:41 UTC
Read the original article Hit count: 743

Filed under:
|
|

There was a message posted to the Rhino.Mocks forums yesterday about verifying a single parameter of a method that accepted 5 parameters.  The code looked like this:

 
[TestMethod]
public void ShouldCallTheAvanceServiceWithTheAValidGuid()
{
    _sut.Send(_sampleInput);
    _avanceInterface.AssertWasCalled(x => x.SendData(
        Arg<Guid>.Is.Equal(Guid.Empty),
        Arg<string>.Is.Anything,
        Arg<string>.Is.Anything,
        Arg<string>.Is.Anything,
        Arg<string>.Is.Anything));
}

Not the prettiest code, but it does work.

I was going to reply that he could use the “GetArgumentsForCallsMadeOn” method to pull out an array that would contain all of the arguments.  A quick check of “args[0]” would be all that he needed.  But then Tim Barcz replied with the following:

Just to help allay your fears a bit...this verbosity isn't always a bad thing.  When I read the code, based on the syntax you have used I know that for this particular test no parameters matter except the first...extremely useful in my opinion.

An excellent point!  We need to make sure our unit tests are as clear as our code.

Technorati Tags: ,

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about unittesting