【问题标题】:Unit Test an Event Listener is attaching/detaching an event to a class单元测试事件侦听器正在将事件附加/分离到类
【发布时间】: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


【解决方案1】:

要断言事件处理程序运行,让您附加的处理程序执行您可以断言的操作,例如将布尔值 handlerExecuted 变量从 false 翻转到 true

要断言它是分离的,做同样的事情,但反转断言。 (断言handlerExecutedfalse 而不是true。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多