c# - 使用反射(PrivateObject)进行测试

标签 c# .net testing reflection ref

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

我正在使用 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/

相关文章:

c# - 调用 View 并等到窗口关闭在 MVVM 中可行吗?

.net - Azure Functions 在函数内调用 http post

iphone - iPhone 中的 iAd 集成

javascript - 如何使用 Protractor 测试来确定是否发布我的应用程序?

c# - 如何在 C# 中根据索引拆分列表?

c# - 在 C# 的 linq 中选择大小写

c# - Razor View 的有界属性在发布后未更新

c# - ILOnly = 0 是否表示 C++/CLI?

.net - WCF SOAP 操作

python - gRPC Python : Can't instantiate abstract class ServicerContext