Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Testing Accessing private members of base classes YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

Accessing private members of base classes
In the previous text on PrivateObject and PrivateType we saw how PrivateObject can be used to access private and protected members of an object under test. However, sometimes, you may also need to access some of the members of the object's ancestor. PrivateObject can still be used in that scenario - you just need to construct it using a PrivateType.

Let's say that you have these two dummy classes:
  Public Class X
  
    Private _valueX As Integer
  
  End Class
  
  Public Class Y
    Inherits X
  
    Private _valueY As Integer
  
  End Class
And you're testing Y; let's assume that to make the test work, you need to set _valueX to some specific value.

(And, as in the previous case, I don't care one little bit whether this is sound test design, or whether classes should be reworked so that accessing private members is not necessary, and so on - I fully agree; BUT this is about the situation where you actually DO have to access ancestor's private members.)

This is the standard way of accessing private members:
  <TestMethod()>
  Public Sub TestY()
    Dim y As New Y
    Dim yAccessor As New PrivateObject(y)
  
    yAccessor.SetField("_valueX", 5)
  
    . . .
  End Sub
But if you try to execute this, you'll get an exception similar to this one:

System.MissingFieldException: Field 'Y._valueX' not found.

A way to get around this is to use a different constructor for PrivateObject, one that takes a PrivateType parameter:
  <TestMethod()>
  Public Sub TestYAsX()
    Dim y As New Y
    Dim yAccessor As New PrivateObject(y, New PrivateType(GetType(X)))
  
    yAccessor.SetField("_valueX", 5)
  
    . . .
  End Sub
What this basically does is it tells the accessor that even though you have an instance of class Y there, it should access its non-public fields using X's declaration, not Y's. (This also means that you can't access _valueY using this second version of yAccessor - after all, it's not a part of X's declaration...)

Note that you don't have to do any of this for protected members - if _valueX was protected, you could've just used the first version of TestMethod to access it.

Happy testing!

Top

Comments
Alas!
No comments yet...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

Testing

VB.NET


Related pages

AssertWasCalled and Methods Called Multiple Times

AssertWas[Not]Called and Object Properties

Rhino Mocks's AssertWasNotCalled

Visual Studio - moving coded UI tests to a new / different project results in null reference exceptions

PrivateObject and Out/ByRef parameters

PrivateObject, WithEvents, and generics

CA1800:DoNoCastUnnecessarily

PrivateObject and WithEvents

The creator of this fault did not specify a Reason.

Accessing private and protected members - PrivateObject and PrivateType

VS - Test Run Error - "COM object that has been separated from its underlying RCW cannot be used"

Saving / restoring window placements in .NET

Get the TreeViewItem for an Item in a WPF TreeView

Output in MSTest Tests (VS 2010)

Automated WPF tests and "Invalid URI: Invalid port specified."

Checking Property Change Notifications

Checking "Dangling" Event Handlers in Delphi Forms

Rhino Mocks's AssertWasCalled in VB.NET

First steps with Rhino Mocks (in VB.NET)

Meaningful identifiers

Public fields vs. properties

VS Pending Tests

Automated GUI Testing

Automated GUI Testing in VMs

Automated Testing of Window Forms

Detecting Memory Leaks with DUnit