Check if a property throws an exception using nunit

it seems that there are no delegates. Is there any convenient way to do the following?

Assert.Throws<InvalidOperationException>( delegate { // Current is a property as we all know nullNodeList.GetEnumerator().Current; }); 
+7
c # properties delegates
source share
4 answers

Fast forward four years and now NUnit supports this (current version v2.6 - I did not check which version was introduced).

 Assert.That(() => nullNodeList.GetEnumerator().Current, Throws.InvalidOperationException); 
+7
source share
 Assert.Throws<InvalidOperationException>( delegate { object current = nullNodeList.GetEnumerator().Current; }); 
+6
source share

why not say:

 Assert.Throws<InvalidOperationException>( () => nullNodeList.GetEnumerator().Current); 
+1
source share

You can try to assign it to a variable or try to list:

 Assert.Throws<InvalidOperationException>(delegate { // Current is a property as we all know object current = nullNodeList.GetEnumerator().Current; }); 
+1
source share

All Articles