I came across a script that I am not too sure how to approach.
We are trying to get unit test code coverage of up to 100%.
I am trying to use the TDD approach to development, write tests, do it, write a failed test, add more code to make it pass, etc.
In doing so, I found that I wrote a class like this:
public enum IntervalType : int { Shift = 1, PaidBreak = 2, UnpaidBreak = 3 } public static class QuarterFactory { public static IQuarter CreateQuarter(IntervalType intervalType) { switch (intervalType) { case IntervalType.Shift: return new ShiftQuarter(); default: throw new ArgumentException(String.Format("Unable to create a Quarter based on an IntervalType of: {0}", intervalType)); } } }
As coding progressed, the factory extension was expanded:
public static class QuarterFactory { public static IQuarter CreateQuarter(IntervalType intervalType) { switch (intervalType) { case IntervalType.Shift: return new ShiftQuarter(); case IntervalType.PaidBreak: return new PaidBreakQuarter(); case IntervalType.UnpaidBreak: return new UnpaidBreakQuarter(); default: throw new ArgumentException(String.Format("Unable to create a Quarter based on an IntervalType of: {0}", intervalType)); } } }
The first question I ask is:
Now that the factory is doing an enumeration, do I remove the default exception to cover the code, or keep the exception there by default if someone adds a new type to the enumeration and forgets to change the factory?
The second question I ask is:
If I decided to remove the exception and use the UnpaidBreakQuarter type by default - does it make sense to return IQuarter to UnpaidBreakQuarter by default, or it will raise the question of why UnpaidBreakQuarter is used by default, is there a business rule for this? "
Hello,
James