EventHandlers 的单元测试和代码覆盖率
Unit test and code coverage on EventHandlers
我正在尝试对 class 中的 eventHandler 进行单元测试,但我不确定如何获取有效数据以放入测试中。提前致谢!
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 以下编辑是针对评论中讨论的错误
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. I hope it makes sense.
这是一个可能有用的简单测试
[Test]
public void should_raise_event()
{
BriefAssociation.BriefAssociationChangedEvent += BriefAssociationChanged;
bool result = BriefAssociation.HasListener(null);
Assert.True(result);
}
public void BriefAssociationChanged(Object obj, AssociationEventArgs associationEventArgs)
{ }
以下是第一种方法所需的所有测试:
[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);
}
我正在尝试对 class 中的 eventHandler 进行单元测试,但我不确定如何获取有效数据以放入测试中。提前致谢!
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 以下编辑是针对评论中讨论的错误
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. I hope it makes sense.
这是一个可能有用的简单测试
[Test]
public void should_raise_event()
{
BriefAssociation.BriefAssociationChangedEvent += BriefAssociationChanged;
bool result = BriefAssociation.HasListener(null);
Assert.True(result);
}
public void BriefAssociationChanged(Object obj, AssociationEventArgs associationEventArgs)
{ }
以下是第一种方法所需的所有测试:
[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);
}