我有一个小但很烦人的问题。
我正在使用 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);
}
问题:为什么测试方法中的device obj没有设置为null?
感谢任何帮助
亲切的问候 卡斯滕
最佳答案
返回是通过传入 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);
}
关于c# - 使用反射(PrivateObject)进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382948/