Skorunka František Skorunka František - 3 months ago 20
C# Question

How do you Unit TEst code which uses String.IsNullOrEmpty?

Do you write two Unit Tests? One for value being Null and one for value being string.Empty? The similar for string.IsNullOrWhiteSpace()?

Answer

Typically I'd use NUnit test cases to test these permutations. They'll give you coverage of the three different checks without duplicating your test code.

For example:

[TestCase("")]
[TestCase(null)]
public class SomeTest(string stringValue)
{
   Assert.Throws<ArgumentException>(()=> CheckIfNullOrEmpty(stringValue));
}

public void CheckIfNullOrEmpty(string val)
{
   if(string.IsNullOrEmpty(val))
   {
       throw new ArgumentException();
   }
}