I had to write a unit test for an existing method which maps an enum to another enum. This unit test is concerned about the scenario when the mapping is not defined in the method, and we get an exception in the default block of the switch statement.
enum players{sachin, ponting, mculum, gayle}
enum teams{westindies, australia, india, newzealand, southafrica}
public teams MappingMethod(players p)
{
switch(p)
{
case sachin: return india;
case gayle: return westindies;
......
default: throw new ArgumentOutOfRangeException();
}
}
I tried the unit test with ExpectedException attribute and the unit test worked fine when we had the above scenario. But it fails when the mapping exists for all the items in the enum.
To resole this, I had used a try..catch block inside the unit test and used Assert.IsInstanceOfType to check for the exception instead of using the ExpectedException attribute.
Is there any other better way to do this unit testing?