Dean
Dean

Reputation: 5946

Weird nUnit unit test failure

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!

Upvotes: 0

Views: 1183

Answers (3)

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7856

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

Upvotes: 0

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7856

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")>

Upvotes: 2

Dean
Dean

Reputation: 5946

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

Upvotes: 0

Related Questions