Q: How do I see non-public stuff in a unit test?
Suppose we have a class like the following:
Public Class ToTest
Sub SetValue(y as Integer)
x = y
End Sub
Private x As Integer
End Class
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 privTarget As 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()> _
Public Sub SetValueTest()
Dim target As ToTest = New ToTest
Dim y As Integer = 3
target.SetValue(y)
Dim privTarget As VBClassLib_ToTestAccessor = New VBClassLib_ToTestAccessor(target)
Assert.AreEqual(3, privTarget.x, "x needs to be 3")
End Sub