zoukankan      html  css  js  c++  java
  • 单元测试学习:无返回值,触发委托

    要测试以下方法:

    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 实例。

  • 相关阅读:
    迁移学习
    GAN
    PCA
    LSTM
    hdu 1754 I Hate It 线段树
    hdu 4292 Food 最大流
    hdu 2222 Keywords Search AC自动机
    hdu 3572 Task Schedule hdu 2883 kebab 最大流
    poj 1966 Cable TV Network 点连通度
    hdu 2236 匹配
  • 原文地址:https://www.cnblogs.com/donhwa/p/1686445.html
Copyright © 2011-2022 走看看