1

I have a minor but rather annoying problem.

I am doing some tests using a PrivateObject to access various methods in a class. This all works fine. However when the method signature contains "ref" the ref keyword does not seem to have any effect.

private bool NewDeviceArrivedDeviceAtWorkcenter(ThreadStartArgs args, ref Device deviceAtStation)
{
//..SomeCode
     deviceAtStation = null;
//...Method to test
}

This test is failing..

 [TestMethod]
        public void CheckForDeviceAtWorkcenterNoDeviceFound()
        {
Initialization omitted

var device = new Device();

            var result = accessor.Invoke("NewDeviceArrivedDeviceAtWorkcenter", 
                new []
                    {
                        typeof (ThreadStartArgs), 
                        typeof (Device).MakeByRefType()
                    }, 
                    new object[] 
                    {
                        threadStartArgs, 
                        device
                    });

            Assert.IsNull(device);
}

Question: Why is device obj in the test method not set to null?

Any help appreciated

Kind Regards Carsten

2 Answers2

1

The return is made through the argument array passed into the Invoke.

[TestMethod]
public void CheckForDeviceAtWorkcenterNoDeviceFound()
{ 
   //Initialization omitted for publicObject, threadStartArgs, device

   Type[] myTypes = new Type[] {typeof (ThreadStartArgs), 
                                typeof (Device).MakeByRefType() };
   object[] myArgs = new object[] { threadStartArgs, device };
   string sMethod = "NewDeviceArrivedDeviceAtWorkcenter";

   //Invoke method under test
   bool bResult = (bool)publicObject.Invoke(sMethod, myTypes, myArgs);

   Device returnDevice = (Device)myArgs[1];

   Assert.IsNull(returnDevice);
}
0

According to this reply you should get the MethodInfo of the method you want to test and invoke it just with the parameters array.

Have you tried invoking the method just with typeof(Device), without the MakeByRefType() call?

Community
  • 1
  • 1
m0sa
  • 10,712
  • 4
  • 44
  • 91