如何实例化 DataReceivedEventArgs 或能够用数据填充它?
我正在启动一个进程并重定向错误流,以便能够解析它并知道发生了什么。我这样做是这样的:
_proc.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
其中 NetErrorDataHandler 具有以下签名:
private void NetErrorDataHandler(object sendingProcess, DataReceivedEventArgs errLine)
到目前为止,我必须使用 DataReceivedEventArgs,但我不确定如何测试它。目前我正在运行我正在使用的流程。您无法使用附加数据创建 DataReceivedEventArgs 实例,那么我该如何克服这个障碍呢?我现在看到如何做到这一点的唯一方法是创建一个可以为我完成工作的流程。但是,这并不是用于测试目的的最佳选择。
I'm starting a process and redirecting the error stream to be able to parse it and know what happened. I'm doing it this way:
_proc.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
Where NetErrorDataHandler has the following signature:
private void NetErrorDataHandler(object sendingProcess, DataReceivedEventArgs errLine)
So far I have to work with DataReceivedEventArgs, but I'm not sure how to test it. Currently I'm running the process I'm working with. You can`t create instance of DataReceivedEventArgs with additional data, so how can I overcome this obstacle? The only way I see how to do this now is to create a process that would do the work for me. However, this isn't the best option for testing purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚发现可以使用内部构造函数创建对象并设置内部字段。显然,这是不鼓励的,但它适用于短期嘲笑。这是一个例子:
这样使用:
...这需要一些研究,我认为这是不可能的:-)
I just found out that it is possible to create objects with internal constructors and set the internal fields. Obviously, this is discouraged but it would work for short term mocking. Here's an example:
Use as such:
...this took a bit of research, I didn't think it was possible :-)
您的应用程序使用 DataReceivedEventArgs 中的哪些数据?
最好的解决方案可能是使用一个类包装 DataReceivedEventArgs,您可以将数据注入到该类中进行测试,然后将其传递给实现方法,而不是在当前事件处理程序内完成所有工作。
让我告诉你。首先,创建自定义包装类:
接下来,将事件处理程序实现移至新方法中:
最后,将事件处理程序设置为除了调用新方法外不执行任何操作:
现在,您可以通过调用实现来测试您的实现,如下所示:
What data does your application use out of DataReceivedEventArgs?
Your best solution may be to wrap DataReceivedEventArgs with a class that you are able to inject data into for testing and then pass that to an implementation method instead of doing all of your work inside of the current event handler.
Let me show you. First, create your custom wrapper class:
Next, move your event handler impelmentation into the new method:
Finally, set your event handler to do nothing but call the new method:
Now, you can test your implementation by calling your implementation like this: