如何创建具有私有构造函数的类型实例以进行单元测试?
我需要对响应 System.IO.Ports.SerialPort 的 SerialDataReceived 事件的事件处理程序进行单元测试。该事件处理程序具有签名,
void SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
因此当我的单元测试调用该方法时,它需要一个 SerialDataReceivedEventArgs
实例。但该对象有一个私有构造函数。那么如何将 SerialDataReceivedEventArgs
传递到该方法中呢?
我确信我一定在这里错过了一项明显的技术,我已经度过了漫长的一天......请问有什么建议吗?
I need to unit test an event handler that responds to the SerialDataReceived event of System.IO.Ports.SerialPort
. This event handler has the signature
void SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
So when my unit test calls into the method, it needs a SerialDataReceivedEventArgs
instance. But that object has a private constructor. So how do I get a SerialDataReceivedEventArgs
to pass into the method?
I'm sure I must be missing an obvious technique here, I have had a long day... Any advice please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用反射创建所需类型的实例:
You can create an instance of the desired type using reflection:
您不能直接使用私有构造函数创建类的实例。您可以使用反射来做到这一点,但这是一个非常糟糕的解决方案。
You can't create instances of classes with private constructors directly. You could use reflection to do it, but that is a pretty nasty solution.
我最终重构了我的代码以消除私有构造函数,如下所示:
现在我可以对
SerialDataReceived
进行单元测试,并直接向其传递SerialData
参数。但是,我认为我更喜欢 Alexander 的解决方案,因为它不需要我重构我的生产代码。
I eventually refactored my code to eliminate the private constructor, like this:
So now I can do my unit testing on
SerialDataReceived
and pass it theSerialData
parameter directly.However, I think I prefer Alexander's solution because it doesn't require me to re-factor my production code.