Today I was adding some new logic to some older code and I noticed there was very little test coverage on the logic so I decided to add some. As I was adding coverage I wanted to add some tests that proved out our Contract style bound checks. However there was one slight problem. The method(s) I wanted to add coverage to was overloaded as such:
public virtual IMessageTransporter GetTransporter( Destination destination )
{
// The null transporter will allow for testing without hittng rhapsody or an engine
if ( ConfigurationReader.UseNullTransporter ) { return new NullTransporter(); }
Contracts.Requires( destination != null );
// for now we only have one transporter for now.... will add more later
return new HttpTransporter( destination.Endpoint );
}
and
public virtual IMessageTransporter GetTransporter( Endpoint endpoint )
{
// The null transporter will allow for testing without hittng rhapsody or an engine
if ( ConfigurationReader.UseNullTransporter ) { return new NullTransporter(); }
Contracts.Requires( endpoint != null );
// for now we only have one transporter for now.... will add more later
return new HttpTransporter( endpoint );
}
As you can see from the 2 methods above they are the same with ONLY the provided parameter being different. When I started to write my tests I did this:
transporterFactory.GetTransporter( null );
But when I simply added NULL as the parameter the compiler provided me the following error
The call is ambiguous between the following methods or properties: ‘TransporterFactory.GetTransporter(Destination)' and TransporterFactory.GetTransporter(Endpoint)' C:\PathHere\TransporterFactoryTests.cs 13 45 Common.Tests
Now if you think about it for even 2 seconds the above error makes perfect sense. So the real question is how do you get around this….? Simple you do the following.
transporterFactory.GetTransporter( (Destination)null );
By providing a type on your call the compiler now will know which overload you are attempting to call and the world will be a happier place.
Till next time,
Posted
01-21-2010 6:08 AM
by
Derik Whittaker