I would like to ask how you would test a button click e开发者_如何学编程vent 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?
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
精彩评论