2011-12-05 80 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); 
}