测试弹出表单的按钮单击事件时忽略 Form.ShowDialog() 吗?
我想问您如何测试按钮单击事件,在显示表单后您想检查结果。我正在使用 NUnit 进行测试。我创建了一个扩展方法来显示如下形式,尝试了不同的方法,例如检查 Debugger.IsAttached?;
public static void ShowFormDialog(this Form form)
{
if(Debugger.IsAttached)
form.ShowDialog();
}
但这似乎在运行我的测试时仍然弹出表单。我还有其他方法可以做到这一点吗?
I would like to ask how you would test a button click event where by you want to check the results after having shown a form. I am using NUnit to test. I created an extension method to show the form as below,have tried different methods such as checking Debugger.IsAttached?;
public static void ShowFormDialog(this Form form)
{
if(Debugger.IsAttached)
form.ShowDialog();
}
but this appears to still popup the form while running my test. Is there any other way I could do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的设计可能需要一些工作。你在测试什么?处理表单事件的表单或类?如果这两者没有分开,那就应该分开。
如果是后者,那么您应该使用依赖项注入,这样您就可以存根表单,并以您自己的方式引发事件。
如果没有非常充分的理由(还没有看到足够充分的理由),永远不要使用像 Debugger.IsAttached 这样的条件。
问候,
莫滕
Your design might need some work. What are you testing ? The form or the class that handles the form events? If those two are not separated, it should be.
If the latter, then you should use dependency injection, so you can stub the form, and raise the event in your own way.
Using conditionals like Debugger.IsAttached should never be used without extremely good reasons (haven't seen a good enough reason yet).
Regards,
Morten