如何编写单元测试取决于MessageBox?

如何编写单元测试取决于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.