简体   繁体   English

EventHandlers的单元测试和代码覆盖率

[英]Unit test and code coverage on EventHandlers

I'm trying to do a unit test on the eventHandler in the class and I'm not exactly sure how to get valid data to put in the tests. 我正在尝试对类中的eventHandler进行单元测试,但我不确定如何获取有效数据以放入测试中。 Thanks in advance! 提前致谢!

public class BriefAssociation
    {

        public static event EventHandler<AssociationEventArgs> BriefAssociationChanged;
        public static event EventHandler<AssociationEventArgs> BriefAssociationChangedEvent;

        public static void OnBriefAssociationChanged(AssociationEventArgs e)
        {
            BriefAssociationChanged(null, e);
        }

        public static bool HasListener(EventHandler<AssociationEventArgs> TestCheck)
        {
            if ((BriefAssociationChangedEvent != null))
                if ((BriefAssociationChangedEvent.GetInvocationList().Length > 0))
                {
                    return true;
                }

            return false;
        }
    }


    public class AssociationEventArgs
    { 
        public int CustomerID;
    }

CHANGES The following edit is for an error which is discussed in the comments 更改下面编辑为这是在评论中讨论的错误

public class BriefAssociation
{

    public static event EventHandler<AssociationEventArg> BriefAssociationChanged;
    public static event EventHandler<AssociationEventArg> BriefAssociationChangedEvent;

    public static void OnBriefAssociationChanged(AssociationEventArg e)
    {
        BriefAssociationChanged(null, e);
    }


    public static bool HasListener(EventHandler<AssociationEventArg> TestCheck)
    {
        if ((BriefAssociationChangedEvent != null))
            if ((BriefAssociationChangedEvent.GetInvocationList().Length > 0))
            {
                return true;
            }

        return false;
    }
}
public class AssociationEventArg
{ 
    public int CustomerID;
}

For the second method "HasListener" I have a test that gives it a null value to test the if statement but I need to give it something that has a value of length grater than 0 to test the rest of the function. 对于第二种方法“ HasListener”,我有一个测试,给它一个空值以测试if语句,但我需要给它提供一个长度值大于0的值,以测试该函数的其余部分。 I hope it makes sense. 我希望这是有道理的。

This is a simple test that might help 这是一个简单的测试,可能会有所帮助

[Test]
public void should_raise_event()
{
    BriefAssociation.BriefAssociationChangedEvent += BriefAssociationChanged;
    bool result = BriefAssociation.HasListener(null);
    Assert.True(result);
}

public void BriefAssociationChanged(Object obj, AssociationEventArgs associationEventArgs)
{ }

Here's all the test you need for the 1st method: 这是第一种方法所需的所有测试:

    [Test]
    public void OnBriefAssociationCHanged_ShouldRaiseBriefAssociationChangedEvent()
    {
        // Data
        object resultSender = null;
        AssociationEventArgs resultArgs = null;
        AssociationEventArgs testArgs = new AssociationEventArgs();

        // Setup
        BriefAssociation.BriefAssociationChanged += (sender, args) =>
        {
            resultSender = sender;
            resultArgs = args;
        };

        // Test
        BriefAssociation.OnBriefAssociationChanged(testArgs);

        // Analysis
        Assert.IsNull(resultSender);
        Assert.AreSame(resultArgs, testArgs);
    }

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

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