使用反射进行测试(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);
}
问题:为什么测试方法中的device obj没有设置为null?
任何帮助表示赞赏
亲切的问候 卡斯滕
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过传递到 Invoke 的参数数组来返回。
The return is made through the argument array passed into the Invoke.
根据 此回复,您应该得到您想要测试的方法的 MethodInfo 并仅使用参数数组调用它。
您是否尝试过仅使用
typeof(Device)
调用该方法,而不使用MakeByRefType()
调用?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 theMakeByRefType()
call?