Testing Private Types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have some simple (private) types...

Friend Enum Types
Value1
Value2
End Enum

Friend Structure Detail
Dim Type As Types
End Structure

Friend Class Parser
Friend Function GetDetail() As Detail
Dim _Detail As New Detail
_Detail.Type = Types.Value2
Return _Detail
End Function
End Class

I hope to test the Parser.GetDetail method with the following test but I
can't see how to set the _ExpectedDetail.Type...

<TestClass()> Public Class ParserTest

<TestMethod()> Public Sub GetDetailTest()

Dim _Parser As ClassLibrary1_ParserAccessor = New
ClassLibrary1_ParserAccessor(ClassLibrary1_ParserAccessor.CreatePrivate)

Dim _ExpectedDetail As ClassLibrary1_DetailAccessor = New
ClassLibrary1_DetailAccessor(ClassLibrary1_DetailAccessor.CreatePrivate)

'_ExpectedDetail.Type = ClassLibrary1_TypesAccessor.Value2
'_ExpectedDetail.Type = New
ClassLibrary1_TypesAccessor(ClassLibrary1_TypesAccessor.Value2)
'_ExpectedDetail.Type = ???

Dim _ActualDetail As ClassLibrary1_DetailAccessor = _Parser.GetDetail

Assert.AreEqual(_ExpectedDetail.Type, _ActualDetail.Type)

End Sub

End Class
 
You can test the friends by embedding your test stubs, although this is not
a "best practice". You can also test with reflection, which is what Team
System does. I do not have the details (i.e. code samples), but I might be
able to find the bandwidth to reverse engineer a Team System test on a
method not publicly exposed.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
Thanks Gregory.

Visual Studio neatly creates the necessary classes to test my private
members. The lines in my example "Dim _Parser As..." and "Dim _ExpectedDetail
As..." construct special accessor objects that VS defines. These allow access
to my private objects and allow me to test most of their members.

However, I can't see how to test (i.e. set) a property on one of these
accessor objects when the property itself is typed using another private
type. The two commented lines in my example "ExpectedDetail.Type = ..." show
a couple of attempts that I had at this, but both fail with exceptions!
 
Hello,

I think you may set the expected value directly with the enum values, for
example:

Assert.AreEqual(ClassLibrary1_TypesAccessor.Value2, _ActualDetail.Type)

Can this help on the issue?

Luke Zhang
Microsoft Online Community Lead

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Luke

Although this works it doesn't quite do what I want.

I want to test the detail object as a whole, not test each property
separately, i.e. I want to do...

Assert.AreEqual(_ExpectedDetail, _ActualDetail)

....not...

(Pseudo code)
For Each Property in _ExpectedDetail.Properties
Assert.AreEqual(Property, ActualDetail.CorrespondingProperty)
Next Property

This doesn't matter so much in my example because the Detail type only has
one member but in my real problem it has many members.

Do you have any other suggestions?
 
Or you may add a method in the structure/class to set internal fields, like:

Friend Structure Detail
Dim Type As Types

Friend Sub SetType(ByVal value As Types)

Type = value
End Sub

End Structure

In the Unit Test code:

_ExpectedDetail.SetType(ClassLibrary1_TypesAccessor.Value2)

Regards,

Luke Zhang
Microsoft Online Community Lead

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
OK that works although it isn't ideal because I have to change the primary
class to facilitate the test class.

Can you explain why setting the Type member directly (i.e.
_ExpectedDetail.Type = ClassLibrary1_TypesAccessor.Value2) doesn't work?
 
Hello,

Thank you for the confirmation. Regarding the question
"_ExpectedDetail.Type" is defined as ClassLibrary1_TypesAccessor, but
ClassLibrary1_TypesAccessor.Value2 is actually ClassLibrary1.Types (got
from Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject with
reflection). So, it cannot be assigned the value directly.

Luke Zhang
Microsoft Online Community Lead

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top