使用反射进行测试(PrivateObject)

发布于 2024-12-19 16:08:51 字数 1002 浏览 2 评论 0原文

我有一个小但相当烦人的问题。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2024-12-26 16:08:51

通过传递到 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);
}

The return is made through the argument array passed into the 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);
}
雾里花 2024-12-26 16:08:51

根据 回复,您应该得到您想要测试的方法的 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 the MakeByRefType() call?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文