使用NUnit 2.6检查异常属性是否相同的最惯用的方法是什么?
代码我想写,但 不 工作:Expected 3, but was <empty>
Assert.That(() => someObject.MethodThrows(),
Throws.TypeOf<SomeException>().With.Property("Data").Count.EqualTo(3), /* Data is a collection */
"Exception expected");
我可以使用嵌套的Assert
表达式,但这似乎过于复杂和不必要:
Assert.AreEqual(3,
Assert.Throws<SomeException>(
() => someObject.MethodThrows(),
"Exception expected").Data.Count);
编辑事实上,第一个代码示例确实有用。在发布此问题之前,我不知道为什么它没有多次工作
答案 0 :(得分:1)
我会这样做:
var exception = Assert.Throws<SomeException>(() => someObject.MethodThrows(),
"Exception expected")
Assert.AreEqual(3, exception.Data.Count);
这是你能得到的最清晰的:
答案 1 :(得分:1)
我无法与NUnit 2.6对话,但在NUnit 2.5上进行了以下测试:
Public Class MyException
Inherits Exception
Public Property SomeList As New List(Of String) From {"hello", "world"}
End Class
<TestFixture()>
Public Class TestClass1
Public Shared Sub DoSomething()
Throw New MyException()
End Sub
<Test()>
Public Sub TestExample()
Assert.That(Sub() DoSomething(), Throws.TypeOf(Of MyException)().With.Property("SomeList").Count.EqualTo(3))
End Sub
End Class
产生以下错误消息:
Expected: <ClassLibrary1.MyException> and property SomeList property Count equal to 3
But was: < "hello", "world" >
这可能只是NUnit 2.6 beta的回归吗?