如何编写单元测试取决于MessageBox? [英] How to write unit test depends on MessageBox?

查看:70
本文介绍了如何编写单元测试取决于MessageBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NUnit来测试单位,我有一个方法:



I use NUnit to test unit and I have a method:

class abc
   {
      private int a;
      public void myMethod()
      {
           if(MessageBox.Show("Sure?","Title", MessageBoxButtons.YesNo) == DialogResult.Yes)
                 a = 1;
           else
                 a = 0;
      }
   }





如何编写NUnit来测试此方法?对不起我的英文。

非常感谢。



How do I write NUnit to test this method? Sorry for my English.
Thank you very much.

推荐答案

你可以看一下NUnitForms [ ^ ]或更改myMethod拿一个标志来说明是否显示消息,例如
You could have a look at NUnitForms[^] or change myMethod to take a flag to say whether or not to show the message e.g.
public int myMethod(bool ShowMessage = true)
{
    int retVal = 1;
    if (ShowMessage)
        retVal = (MessageBox.Show("Sure?", "Title", MessageBoxButtons.YesNo) == DialogResult.Yes) ? 1 : 0;

    return retVal;
}



请注意,不建议以这种方式更改应用程序以适应测试。


Note however, changing the application to suit the test in this way isn't recommended.


这篇关于如何编写单元测试取决于MessageBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆