2011-12-05 45 views
1

我有一个小问题,但相当烦人的问题。使用反射测试(PrivateObject)

我正在做一些测试,使用PrivateObject访问类中的各种方法。这一切工作正常。但是,当方法签名包含“ref”时,ref关键字似乎没有任何作用。

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

此测试失败..

[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); 
} 

问题:为什么在没有设置为空的测试方法设备的obj?

任何帮助表示赞赏

亲切的问候 卡斯滕

回答

0

根据this回复你应该让你想测试,只是与参数数组调用它的方法的MethodInfo。

您是否尝试过使用typeof(Device)调用该方法,而不调用MakeByRefType()

1

返回是通过传入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); 
}