C# 使用反射进行测试(PrivateObject)
我有一个小问题,但相当烦人 我正在使用PrivateObject进行一些测试,以访问类中的各种方法。这一切都很好。但是,当方法签名包含“ref”时,ref关键字似乎没有任何效果C# 使用反射进行测试(PrivateObject),c#,.net,testing,reflection,ref,C#,.net,Testing,Reflection,Ref,我有一个小问题,但相当烦人 我正在使用PrivateObject进行一些测试,以访问类中的各种方法。这一切都很好。但是,当方法签名包含“ref”时,ref关键字似乎没有任何效果 private bool NewDeviceArrivedDeviceAtWorkcenter(ThreadStartArgs args, ref Device deviceAtStation) { //..SomeCode deviceAtStation = null; //...Method to test
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未设置为空
谢谢你的帮助
问候
Carsten根据回复,您应该获得要测试的方法的MethodInfo,并仅使用参数数组调用它
您是否尝试仅使用typeof(Device)
调用该方法,而不使用MakeByRefType()
调用?根据回复,您应该获得要测试的方法的MethodInfo,并仅使用参数数组调用它
您是否尝试仅使用
typeof(Device)
调用该方法,而不使用MakeByRefType()
调用?通过传递到调用中的参数数组进行返回
[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);
}
通过传入调用的参数数组返回
[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);
}