【问题标题】:Weird nUnit unit test failure奇怪的 nUnit 单元测试失败
【发布时间】:2010-09-21 01:22:26
【问题描述】:

我有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 not declared 错误!

【问题讨论】:

    标签: vb.net unit-testing nunit


    【解决方案1】:

    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
    

    另见这些文章

    【讨论】:

      【解决方案2】:

      好的。运行这个。

      异常的消息是:

      行号超过 4 个字符

      参数名称:aaaa

      (包括换行符)

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

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

      【讨论】:

        【解决方案3】:

        我不确定我是否同意您对不推荐使用 ExpectedException 属性的评论。

        它在 2.4(我正在使用的版本)中仍然得到很好的支持,在这种情况下是 ExpectedMessage 导致了弃用问题

        uUnit Exception Guide

        【讨论】:

          猜你喜欢
          • 2014-10-01
          • 1970-01-01
          • 2021-07-17
          • 1970-01-01
          • 2020-06-22
          • 1970-01-01
          • 2021-10-11
          • 1970-01-01
          • 2019-03-04
          相关资源
          最近更新 更多