apache-flex - 柔性,柔性单元 : How to test that an event is dispatched twice?

标签 apache-flex unit-testing events testing flexunit

我正在 Flex 应用程序中测试一些事件分派(dispatch)代码,使用 FlexUnit 的 addAsync 方法测试事件是否已分派(dispatch)。到目前为止很棒,我可以确保至少触发了一个事件。但是,我想更详细一点;我想确保准确地派发我期望的事件集。是否有有用的测试模式(或者,甚至不同的测试框架——我很灵活!)来完成这个?

我试过这段代码,但它似乎没有被第二次调用:

protected function expectResultPropertyChange(event: Event, numberOfEvents: int = 1): void {
    trace("Got event " + event + " on " + event.target + " with " + numberOfEvents + " traces left...");
    assertTrue(event.type == ResponseChangedEvent.RESPONSE_CHANGED);
    if (numberOfEvents > 1) {
        event.target.addEventListener(ResponseChangedEvent.RESPONSE_CHANGED, addAsync(expectResultPropertyChange, 1000, numberOfEvents - 1));
    }
}

public function testSomething(): void {
    requiredQuestion.addEventListener(ResponseChangedEvent.RESPONSE_CHANGED, addAsync(expectResultPropertyChange, 1000, 2));
    requiredQuestion.responseSelected("1", true);
    requiredQuestion.responseSelected("2", true);
}

最佳答案

回应评论...

What if the event is dispatched directly? responseSelected doesn't trigger an asynchronous event on a composite object, it simply dispatched the RESPONSE_CHANGED event itself directly. I'm not seeing how this approach can be mocked using your method. Mind you, I'm fuzzy on the mock testing practice as-is, so I'm probably missing a simple solution here.

..在这种情况下,您不需要使用模拟或 addAsync。像这样的事情会做:

public function testSomething(): void 
{
    var requiredQuestion : RequiredQuestion = new RequiredQuestion();

    var callCount : int = 0;
    requiredQuestion.addEventListener(ResponseChangedEvent.RESPONSE_CHANGED, function(event : ResponseChangedEvent)
    {
        callCount++;
    });

    requiredQuestion.responseSelected("1", true);
    requiredQuestion.responseSelected("2", true);

    assertEquals(2, callCount);
}

关于apache-flex - 柔性,柔性单元 : How to test that an event is dispatched twice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1167549/

相关文章:

apache-flex - Flex 4 应用空白

angularjs - 在 UI-Router 中获取当前 Controller 实例? (在测试中)

c - 如何在 GTK 中从其他窗口生成窗口? (使用C)

git - 滥用 Git 来实现事件存储架构?

php - 在Web服务器上部署Flex应用程序问题

python - 使用 python cgi 发送 .png 文件

apache-flex - Flex数据网格: Change value based on another value?

c# - 模拟一个对其参数有副作用的方法

unit-testing - 如何防止在 Visual Studio 2012 中自动执行单元测试?

javascript - 如何以编程方式更新全日历中事件的呈现