atamanroman
atamanroman

Reputation: 11818

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;
       });

Upvotes: 9

Views: 3537

Answers (4)

EddPorter
EddPorter

Reputation: 188

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);

Upvotes: 11

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

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;
});

Upvotes: 1

Anton Gogolev
Anton Gogolev

Reputation: 115877

Assert.Throws<InvalidOperationException>(
    delegate { object current = nullNodeList.GetEnumerator().Current; });

Upvotes: 8

sukru
sukru

Reputation: 2259

why not say:

Assert.Throws<InvalidOperationException>(
    () => nullNodeList.GetEnumerator().Current);

Upvotes: 0

Related Questions