开发者

Test if property throws exception with nunit

开发者 https://www.devze.com 2023-01-09 07:08 出处:网络
it seems there are no delegates to properties. Is there a convenient way to do the following? Asse开发者_运维技巧rt.Throws<InvalidOperationException>(

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

Asse开发者_运维技巧rt.Throws<InvalidOperationException>(
       delegate
       {
           // Current is a property as we all know
           nullNodeList.GetEnumerator().Current;
       });


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


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


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


why not say:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号