Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 699
Svish Avatar asked Jan 11 '10 12:01

Svish


People also ask

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C or C++ same?

The main difference between C and C++ is that C is a procedural programming language that does not support classes and objects. On the other hand, C++ is an extension of C programming with object-oriented programming (OOP) support.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


2 Answers

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...

like image 60
Svish Avatar answered Sep 21 '22 20:09

Svish


If you want to do more with the exception than just assert that it is thrown, then Assert.Throws actually returns the exception and you can do this:

var exception = Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog));
// Assert something else about the exception
like image 41
David M Avatar answered Sep 23 '22 20:09

David M