要测试以下方法:

private void DoAct(string msg)
{
    ActMessage(msg);
}

public Action<string> ActMessage;

 

测试无返回值的方法主要是否引发了异常,有没有对非空做处理:

public void DoActTest()
{
    Form1_Accessor target = new Form1_Accessor(); 
    Exception ex = null;
    string msgResponse = string.Empty;
    string msgPost = string.Empty; 

    try
    {
        target.DoAct(msgPost);
    }
    catch (Exception e)
    {
        ex = e;
    }

    Assert.IsNull(ex, "异常,测试失败!");
}    
二,测试被测方法是否正正触发了委托:
public void DoActTest()
{
    Form1_Accessor target = new Form1_Accessor(); 
    Exception ex = null;
    string msgResponse = string.Empty;
    string msgPost = string.Empty;
    target.ActMessage = (m) => { msgResponse = m; };
    try
    {
        target.DoAct(msgPost);
    }
    catch (Exception e)
    {
        ex = e;
    }

    Assert.IsNull(ex, "异常,测试失败!");            
                
    Assert.AreEqual(msgPost, msgResponse);
}

一般我的做法是,在DoAct 中需要判断下 ActMessage 是否为空的判断。此处DoAct是私有方法,VS 产生的是一个私有方法访问器,如上的 Form1_Accessor,而不是公用方法的 Form1 实例。

相关文章:

  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-09-04
  • 2021-06-23
  • 2021-11-10
猜你喜欢
  • 2021-08-30
  • 2021-10-02
  • 2022-12-23
  • 2022-02-07
  • 2021-04-08
  • 2021-07-29
  • 2021-07-17
相关资源
相似解决方案