8protons
8protons

Reputation: 3989

NUnit TestCase Cannot Refer to Instance Member

I'm trying to SetUp an object for use in some Visual Basic NUnit integration tests similarly to how I've done it before in C#.

Public Class ApprovalLevelTests

Private myLevel As ApprovalLevel= Nothing

<SetUp>
Public Function Setup()
    myLevel = ApprovalLevel.GetApprovalLevel(1, "414", "MKRT")
End Function

Public Sub TearDown()

End Sub

<TestCase(myLevel.Basic, "<=$1,000")>
<TestCase(myLevel.Middle, "$1,000-$5,000")>
Public Sub AutoApprovalRange_ValidRanges_ReturnsTrue(ByVal approvalRange As String, ByVal limit As String)

But Visual Studio complains that myLevel cannot be used in the test case because you cannot refer to an instance member of a class from within a shared method or shared member initializer. Im a bit confused because I tried doing this by passing string literals and it still got upset. If I do it this way, it works no problem.

Public Sub AutoApprovalRange_ValidRanges_ReturnsTrue()
    ApprovalLevel level = ApprovalLevel.GetApprovalLevel(1, "414", "MKRT");
    Assert.IsTrue(level.Basic == "(Limit: <=$1,000)");
    Assert.IsTrue(level.Middle == "(Limit: $1,000-$5,000)");

What am I not understanding?

Upvotes: 1

Views: 145

Answers (1)

Craig
Craig

Reputation: 2494

As you may already know, the descriptions that you provide in angle brackets are .NET Attributes. This is a general feature that the unit testing machinery uses to identify and configure tests. Attributes are effectively Shared as they are a property of the routine across all classes. In fact, they go farther than being Shared as they are a property of the language item in the assembly.

As a consequence of this, the C# documentation on attributes explicitly states that attribute arguments must be compile-time constants. You can find the C# documentation here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes

I did not find anything equivalent in the VB documentation of attributes, but I wouldn't think this would be language-specific. It's a natural conclusion of what attributes are and how they are processed by the compiler.

Upvotes: 1

Related Questions