How do I access private members from a VB.NET test case?
I'm using VS 2010 Professional built-in test cases. I've got a class that has a read-only p开发者_运维知识库roperty that looks something like this:
Private _server As IServer = Nothing
Public ReadOnly Property Server()
Get
If _server Is Nothing Then
RemotingSetup.RegisterHttpBinaryChannel()
_server = RemotingFactory.CreateProxy(Of IServer)(Options.DevLocal)
End If
Return _server
End Get
End Property
In my test cases I want to make a fake server implementation so I can test some of the class methods without actually hitting the server. I've looked around and have seen the InternalsVisibleTo
attribute, but that seems to only allow access to Friend
-level members.
Is there any simple way to get access to these members, aside from declaring them Public/Friend?
Apart from using a mocking library that will mock private members (such as TypeMock, which is the only one I know of that does this), you are out of luck, at least when it comes to testing this particular class directly.
There are other possibilities - you can subclass from this class and only override this property. This will let you test all other functionality. Of course, this assumes that the class isn't sealed (NotInheritable).
精彩评论