ArgumentNullException

I’d never looked too hard at what ArgumentNullException actually throws, including formatting. A test that includes it:

public static List<int> GetRandomInterval(int lowerLimit, int upperLimit, int maxDistance, Random rand) {
          if (rand == null) {
              throw new ArgumentNullException(nameof(rand));
          }

[TestMethod]
     [ExpectedException(typeof(ArgumentNullException))]
     public void PassingInNullRandomToGetRandomIntervalThrowsException() {
         Random rand = null;
         try {
             // ReSharper disable once ExpressionIsAlwaysNull
             NumberUtilities.GetRandomInterval(LowerLimit, UpperLimit, MaxDistance, rand);
         }
         catch (ArgumentNullException e) {
             Assert.AreEqual("Value cannot be null.\r\nParameter name: rand", e.Message);
             throw;
         }
     }

XUnit: testing for exceptions

I think I’ve written about this before… but I can’t find it. Basically I find the paradigm for exception testing nicer in XUnit compared with MsTest, in my view. Note that there is no decoration, and you can test easily both for the exception type, and for a message (in this case) that accompanies it:

[Fact]
public void RequestForNotFoundFrequencyThrowsException() {
TraceExecutingMethod();
var toneSet = new Model.ToneUtility();
Exception ex = Assert.Throws<ArgumentException>(() => toneSet.GetNoteElements(9999));
Assert.Equal("[The frequency [9999] was not found in the set of available notes]", ex.Message);
}