Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if property throws exception with nunit

it seems there are no delegates to properties. Is there a convenient way to do the following?

Assert.Throws<InvalidOperationException>(
       delegate
       {
           // Current is a property as we all know
           nullNodeList.GetEnumerator().Current;
       });
like image 834
atamanroman Avatar asked Jul 30 '10 08:07

atamanroman


3 Answers

Fast-forward four years and NUnit now supports this (current version is v2.6 - I've not checked which version this was introduced).

Assert.That(() => nullNodeList.GetEnumerator().Current,
    Throws.InvalidOperationException);
like image 184
EddPorter Avatar answered Nov 05 '22 18:11

EddPorter


Assert.Throws<InvalidOperationException>(
    delegate { object current = nullNodeList.GetEnumerator().Current; });
like image 35
Anton Gogolev Avatar answered Nov 05 '22 19:11

Anton Gogolev


You could try assigning it to a variable or try enumerating:

Assert.Throws<InvalidOperationException>(delegate
{
    // Current is a property as we all know
    object current = nullNodeList.GetEnumerator().Current;
});
like image 34
Darin Dimitrov Avatar answered Nov 05 '22 17:11

Darin Dimitrov