Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Testing PrivateObject and Out/ByRef parameters YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

PrivateObject and Out/ByRef parameters
I got an interesting question to the previous article on PrivateObject: What about testing private/protected methods with Out/ByRef parameters?

The default approach one might take won't work:
  Public Class X
  
    Private Sub ByRefParam(ByRef param As Integer)
      param = 5
    End Sub
  
  End Class
  
  . . .
  
  <TestMethod()>
  Public Sub TestX1()
    Dim x As New X
    Dim xAccessor As New PrivateObject(x)
  
    Dim param As Integer
  
    xAccessor.Invoke("ByRefParam", {param})
  
    Assert.AreEqual(5, param)
  End Sub
This test will fail with the message that 5 is not equal to 0.

The problem here is that the array of parameters passed by Invoke to ByRefParam (here: {params}) doesn't take a reference to the param local variable, but just its value.

However, the good news is that, in the above, we do have variables that are passed, by reference, to ByRefParam - the individual entries in the array of parameters itself!

This test does pass:
  <TestMethod()>
  Public Sub TestX2()
    Dim x As New X
    Dim xAccessor As New PrivateObject(x)
  
    Dim params As Object() = {Nothing}
    xAccessor.Invoke("ByRefParam", params)
  
    Assert.AreEqual(5, params(0))
  End Sub
Here, we test ByRefParam's parameter as if it were an Out parameter - because that's how it is implemented. And thanks to this, it doesn't really matter what values params are initialized to in the test method...

If ByRefParam was implemented as a non-Out parameter, for instance like this:
  Public Class X
  
    Private Sub ByRefParam(ByRef param As Integer)
      param += 5
    End Sub
  
  End Class
Then both of these would pass:
  <TestMethod()>
  Public Sub TestX3()
    Dim x As New X
    Dim xAccessor As New PrivateObject(x)
  
    Dim params As Object() = {Nothing}
    xAccessor.Invoke("ByRefParam", params)
  
    Assert.AreEqual(5, params(0))
  End Sub
  
  Public Sub TestX4()
    Dim x As New X
    Dim xAccessor As New PrivateObject(x)
  
    Dim params As Object() = {5}
    xAccessor.Invoke("ByRefParam", params)
  
    Assert.AreEqual(10, params(0))
  End Sub
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, WithEvents, and generics

Accessing private members of base classes

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