C #, NUnit: a clear way to test that ArgumentException has the correct name ParamName

To check that something is throwing, for example, an ArgumentException , I can do this:

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

How can I verify that ParamName true in a clear way? And the bonus question: or can you recommend not testing it at all?

+7
c # properties exception nunit
source share
2 answers

Found a pretty clear way (but please let me know if anyone else has the best!)

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

Facepalm ...

+10
source share

If you want to do more with an exception than just claiming it is selected, then Assert.Throws really returns an exception, and you can do this:

 var exception = Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog)); // Assert something else about the exception 
+4
source share

All Articles