Search code examples
c#exceptionpropertiesnunit

C#, NUnit: Clear way of testing that ArgumentException has correct ParamName


To test that something throws for example an ArgumentException I can do this:

Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog));

How can I check that the ParamName is correct in a clear way? And bonus question: Or would you perhaps perhaps recommend not testing this at all?


Solution

  • Found a pretty clear way (but please let me know if anyone have an even better one!)

    var e = Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog));
    Assert.That(e.ParamName, Is.EqualTo("otherDog"));
    

    Facepalm...