奇怪的 nUnit 单元测试失败

发布于 2024-07-07 18:36:48 字数 1633 浏览 10 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-07-14 18:36:48

好的。 只需运行这个即可。

异常的消息是:

行号超过 4 个字符

参数名称:aaaaa

(包括换行符)

您需要将所有这些指定为预期消息:

<ExpectedException(GetType(ArgumentOutOfRangeException), ExpectedMessage="Line No exceeds 4 characters" & VbCrLf & "Parameter name: aaaaa")>

Ok. Just run this.

The message for the exception is:

Line No exceeds 4 characters

Parameter name: aaaaa

(Including the line break)

You need to specify this all of this as the expected message:

<ExpectedException(GetType(ArgumentOutOfRangeException), ExpectedMessage="Line No exceeds 4 characters" & VbCrLf & "Parameter name: aaaaa")>
一抹苦笑 2024-07-14 18:36:48

我不确定我是否同意您对 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

捎一片雪花 2024-07-14 18:36:48

ExpectedExceptionAttribute 已弃用 - 即您根本不应该使用它。 我能很快找到的关于此问题的最佳参考文献是这篇文章(原始文章在这里)。

如果重写,您的单元测试将会更加清晰:

<Test()> _
Sub testLineNoExceedsMaxLength()
    Try

        Dim it As New X("aaaaa", "Test")

    Catch ex as ArgumentOutOfRangeExcpetion

        Assert.That ( ex.Message, Is.Equal("Line No exceeds 4 characters") )

    End Try

End Sub

另请参阅这些文章

The ExpectedExceptionAttribute 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:

<Test()> _
Sub testLineNoExceedsMaxLength()
    Try

        Dim it As New X("aaaaa", "Test")

    Catch ex as ArgumentOutOfRangeExcpetion

        Assert.That ( ex.Message, Is.Equal("Line No exceeds 4 characters") )

    End Try

End Sub

See also these articles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文