似乎没有委托给属性。有没有一个方便的方法来做到以下几点?测试属性是否抛出异常与nunit
Assert.Throws<InvalidOperationException>(
delegate
{
// Current is a property as we all know
nullNodeList.GetEnumerator().Current;
});
似乎没有委托给属性。有没有一个方便的方法来做到以下几点?测试属性是否抛出异常与nunit
Assert.Throws<InvalidOperationException>(
delegate
{
// Current is a property as we all know
nullNodeList.GetEnumerator().Current;
});
快进四年,NUnit现在支持该(当前版本是v2.6 - 我没有检查过这个版本的介绍)。
Assert.That(() => nullNodeList.GetEnumerator().Current,
Throws.InvalidOperationException);
为什么不说:
Assert.Throws<InvalidOperationException>(
() => nullNodeList.GetEnumerator().Current);
Assert.Throws<InvalidOperationException>(
delegate { object current = nullNodeList.GetEnumerator().Current; });
非常感谢。这很容易(但不是很直观) – atamanroman 2010-07-30 08:38:40
@Anton嗨安东我想知道这是用来测试公共类的只读属性的方法吗? – Deeptechtons 2012-07-25 05:05:47
你可以尝试将其分配给一个变量或尝试列举:
Assert.Throws<InvalidOperationException>(delegate
{
// Current is a property as we all know
object current = nullNodeList.GetEnumerator().Current;
});
IM与C#2.0的工作:/ – atamanroman 2010-07-30 08:36:30
您仍然需要做一个实际的(扔掉),分配和保持{},那么:(()=> {VAR X = nullNodeList.GetEnumerator()。当前;}) – nashwan 2013-01-31 16:08:27