Skorunka František Skorunka František - 1 year ago 109
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 Source

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:

public class SomeTest(string stringValue)
   Assert.Throws<ArgumentException>(()=> CheckIfNullOrEmpty(stringValue));

public void CheckIfNullOrEmpty(string val)
       throw new ArgumentException();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download