【发布时间】:2014-06-11 05:03:18
【问题描述】:
我被困在一个单元测试中,测试一个事件是否由事件侦听器附加到一个类并分离。我无法弄清楚如何做到这一点,我已经查看了许多论坛。 EmailEventListener 将构造函数中的事件附加到 fakeClass 并在 Detach() 方法中分离。它还包含触发事件时调用的方法。我正在使用 FakeItEasy 创建 fakeclass。有什么想法吗?谢谢!
[TestMethod]
public void EmailEventListener_AttachedSubscription_ToClass_Successfully()
{
EmailEventListener<ConcreteClass> realListener = new EmailEventListener<ConcreteClass>(fakeClass, A.Dummy<IEmailSender>());
// Assert that fakeClass has the event
realListener.Detach();
// Assert that fakeClass does not have the event (probably in another test, but just wanted to show it here)
}
【问题讨论】:
-
不要测试次要的基础设施,测试当涉及到您的业务逻辑时,该类是否公开了所需的行为。
标签: c# unit-testing events event-listener