简体   繁体   English

C#中的单元测试形式和事件

[英]Unit Testing forms and Events in c#

I want to write Test cases for C# code that triggers events or displays a form to user for input eg: 我想为触发事件或向用户显示表单的C#代码编写测试用例,例如:

private void CreateRecord_Click(object sender, EventArgs e)
{
    try
    {
        this.InitiateRecording();
    }
    catch (BubbleUiException ex)
    {
        objLog.Error(TextRes.IDC_EShuttleError, ex);
        MessageBox.Show(
            ex.Message,
            TextRes.IDC_EShuttleError,
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);
     }
     catch (Exception ex)
     {
         objLog.Error("Error occurred", ex);
         MessageBox.Show(
             ex.Message,
             TextRes.IDC_Error,
             MessageBoxButtons.OK,
             MessageBoxIcon.Error);
     }
 }

How to write Unit tests for these kind of code using Mbunit? 如何使用Mbunit编写此类代码的单元测试?

The "purist" answer is that since it's a private method, it should not be unit tested as it's an implementation detail. “纯粹”的答案是,由于它是私有方法,因此不应将其进行单元测试,因为它是实现细节。 You should aim to test public APIs only. 您应该只测试公共API。

As it is an event handler, you may still want to test it for various reasons. 由于它是事件处理程序,因此出于各种原因,您可能仍要对其进行测试。 As currently written though, this will be hard to do. 如目前所写,这将很难做到。 The fact that you have a 你有一个事实

this.InitiateRecording();

line suggests you haven't properly implemented separation of concerns. 提示您没有正确实施关注点分离。 Your class for handling events also seems to contain code for handling recording. 您处理事件的类似乎还包含用于处理记录的代码。 Secondly, you have hard-coded calls to MessageBox.Show, which will make testing hard as your tests cannot be run in an automatic, stand-alone fashion. 其次,您对MessageBox.Show进行了硬编码的调用,这将使测试变得困难,因为您的测试无法以自动的独立方式运行。

I'd recommend therefore: 因此,我建议:

  1. Refactor the recording functionality out into a separate class, which can be unit tested. 将记录功能重构为一个单独的类,可以对其进行单元测试。
  2. Inject the MessageBox.Show method into that class, so that it can be stubbed during testing. 将MessageBox.Show方法注入该类中,以便可以在测试期间将其存根。
  3. Do not test CreateRecord_Click() as it will simply call a method in your new class. 不要测试CreateRecord_Click(),因为它只会在您的新类中调用一个方法。

Test: 测试:

  1. this.InitiateRecording() is called this.InitiateRecording()被称为
  2. Force a BubbleUiException when this.InitiateRecording() is called BubbleUiException this.InitiateRecording()时强制执行BubbleUiException
  3. Force a Exception that is not BubbleUiException when this.InitiateRecording() is called 调用this.InitiateRecording()时强制Exception不是BubbleUiExceptionException
  4. Wrap MessageBox.Show so you can test that it prints what you expect when the exceptions are thrown. 包装MessageBox.Show以便您可以测试它在抛出异常时可以打印出预期的效果。
  5. Test objLog.Error is called. 测试objLog.Error被调用。

You can assume that your click event works (that the method is called when the control is clicked) as Microsoft have already tested this. 您可以假定您的click事件起作用(当控件被单击时将调用该方法),因为Microsoft已经对此进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM