I have a lump of code that looks a bit like this:
If mode = DiaryMode.Admin Then
modeObject = DBNull.Value
ElseIf mode = DiaryMode.Academy Then
modeObject = ApplicationSettings.Academy
ElseIf mode = DiaryMode.Scouting Then
modeObject = ApplicationSettings.Scouting
Else
Throw New NotSupportedException()
End If
The idea of the check is to prep some values for passing into a database call.
There are two questions, is the Else worth the effort? The intention is to prevent future extensions of the enum causing the code to return squify results.
If the code is valid, I’d like to be able to unit test the behaviour, if it’s worth having it’s worth testing. How might I go about doing that?
If your business requirements state that no other DiaryMode is acceptable, then an else statement is a perfectly fine use to prevent future extension on your DiaryMode enum.
As for testing, that gets a little bit trickier. I would test all valid states (Admin, Academy, and Scouting) for sure. However, you can’t really set the mode to an enum value that doesn’t exist, and that would be the only way to throw the NotsupportedException. I might look into trying to write a test that would verify that the enum only carries the enumerations your expecting.
You could do that by doing something like this:
and then verifying each name.
To sum up, you’d have 4 tests from what I’ve described.