Seeing Non Public stuff in a Unit Test...

Suppose we have a class like the following:

PublicClass ToTest

Sub SetValue(yasInteger)

x = y

EndSub

Private xAsInteger

EndClass

We would like to call SetValue and verify that 'x' gets set.

OK. Right click on SetValue to create a test. Create a new VB Test (or put it into an existing VB Test).

In the generated test, let's set the initialization of 'y' to 3.

Go back to your original code, Right Click on SetValue again, but this time, select the Create Private Accessor command, inserting the accessor into your VB Test Project.

What you want to do is use an accessor to 'wrap' you ToTest object so it can pull private data out of it. This is done follows:

Dim privTargetAs VBClassLib_ToTestAccessor =New VBClassLib_ToTestAccessor(target)

The 'VBClassLib_ToTestAccessor' type I 'discovered' by looking in Class View. I could also have just looked at VSCodeGenAccessor.vb - but this file is really hard to read.

Now you can replace the Assert.Inconclusive at the bottom of the Test Method with:

Assert.AreEqual(3, privTarget.x,"x needs to be 3")

And run the test.

The final code should look like:

'''<summary>

'''A test for SetValue(ByVal Integer)

'''</summary>

<TestMethod()> _

PublicSub SetValueTest()

Dim targetAs ToTest =New ToTest

Dim yAsInteger = 3

target.SetValue(y)

Dim privTargetAs VBClassLib_ToTestAccessor =New VBClassLib_ToTestAccessor(target)

Assert.AreEqual(3, privTarget.x,"x needs to be 3")

EndSub


Joe

[11605 byte] By [JoeRohde] at [2007-12-21]

Visual Studio Team System

Site Classified