奇怪的 nUnit 单元测试失败
我有 classX:
Sub New(ByVal item_line_no As String, ByVal item_text As String)
' check to ensure that the parameters do not exceed the file template limits
Select Case item_line_no.Length
Case Is > m_item_line_no_capacity
Throw New ArgumentOutOfRangeException(item_line_no, "Line No exceeds 4 characters")
Case Else
Me.m_item_line_no = item_line_no
End Select
Select Case item_text.Length
Case Is > m_item_free_text_capacity
Throw New ArgumentOutOfRangeException("Free Text Exceeds 130 characters")
Case Else
Me.m_item_free_text = item_text
End Select
End Sub
和以下 unti 来测试一个故障点
<ExpectedException(GetType(ArgumentOutOfRangeException), "Line No exceeds 4 characters")> _
<Test()> _
Sub testLineNoExceedsMaxLength()
Dim it As New X("aaaaa", "Test")
End Sub
当我运行测试时,我希望得到在异常“行号超过 4 个字符”中抛出的消息
但是单元测试失败并显示以下消息,
RecordTests.testLineNoExceedsMaxLength : FailedExpected exception message: Line No exceeds 4 characters
got: Line No exceeds 4 characters
Parameter name: aaaaa
我认为这很简单,但它让我发疯。
注意:在 ExpectedException 的声明中,我收到一条过时的警告,指出
<ExpectedException(GetType(ArgumentOutOfRangeException), "Line No exceeds 4 characters")>
它应该是
<ExpectedException(GetType(ArgumentOutOfRangeException), ExpectedException="Line No exceeds 4 characters")>
但是这会抛出 ExpectedException is notelasted 错误!
I have classX:
Sub New(ByVal item_line_no As String, ByVal item_text As String)
' check to ensure that the parameters do not exceed the file template limits
Select Case item_line_no.Length
Case Is > m_item_line_no_capacity
Throw New ArgumentOutOfRangeException(item_line_no, "Line No exceeds 4 characters")
Case Else
Me.m_item_line_no = item_line_no
End Select
Select Case item_text.Length
Case Is > m_item_free_text_capacity
Throw New ArgumentOutOfRangeException("Free Text Exceeds 130 characters")
Case Else
Me.m_item_free_text = item_text
End Select
End Sub
and the following unti to test one point of failure
<ExpectedException(GetType(ArgumentOutOfRangeException), "Line No exceeds 4 characters")> _
<Test()> _
Sub testLineNoExceedsMaxLength()
Dim it As New X("aaaaa", "Test")
End Sub
When I run the test I expect to get the message thrown in the exception "Line No exceeds 4 characters"
However the unit test fails with the following message
RecordTests.testLineNoExceedsMaxLength : FailedExpected exception message: Line No exceeds 4 characters
got: Line No exceeds 4 characters
Parameter name: aaaaa
I think the something simple but it driving me insane.
NOTE: in the declaration of the ExpectedException I get an obsolete warning stating that instead of
<ExpectedException(GetType(ArgumentOutOfRangeException), "Line No exceeds 4 characters")>
it should be
<ExpectedException(GetType(ArgumentOutOfRangeException), ExpectedException="Line No exceeds 4 characters")>
However this throws a ExpectedException is not declared error!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的。 只需运行这个即可。
异常的消息是:
(包括换行符)
您需要将所有这些指定为预期消息:
Ok. Just run this.
The message for the exception is:
(Including the line break)
You need to specify this all of this as the expected message:
我不确定我是否同意您对 ExpectedException 属性已弃用的评论。
它在 2.4(我正在使用的版本)中仍然得到很好的支持,在这种情况下是 ExpectedMessage 导致了弃用问题
uUnit 异常指南
I'm not sure I agree with your comment of the ExpectedException attribute being deprecated.
It is still supported perfectly fine in 2.4 (version I am using) it is the ExpectedMessage in this case which is causing the deprecation issue
uUnit Exception Guide
ExpectedExceptionAttribute
已弃用 - 即您根本不应该使用它。 我能很快找到的关于此问题的最佳参考文献是这篇文章(原始文章在这里)。如果重写,您的单元测试将会更加清晰:
另请参阅这些文章
TheExpectedExceptionAttribute
is deprecated - i.e. you shouldn't use it at all. The best reference I could quickly find about this was this post (the original article is here).Your unit test would be a lot clearer if it was re-written:
See also these articles