我在Flex应用程序测试的一些事件分派代码,使用的FlexUnit的addAsync方法的事件分派测试。大到目前为止,我可以保证至少有一个事件被触发。不过,我想更详细一点;我想,以确保完全集我期待分派的事件的。是否有一个有用的测试图案(或者甚至不同的测试框架 - !我是灵活的)?做到这一点。

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

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);
}
有帮助吗?

解决方案

在响应于该评论...

  

如果该事件被分派   直? responseSelected不   触发上的一个异步事件   复合物,它只是出动   在RESPONSE_CHANGED事件本身   直。我没有看到这是如何   方法可以使用被嘲笑   方法。你要知道,我是模糊的   模拟测试实践原样,所以我   可能缺少一个简单的解决方案   这里。

..在这种情况下,你并不需要使用模拟或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);
}

其他提示

这将是怎样一个类似的问题,可以用嘲笑了对象无论它是在做异步调用来解决高层次的例子。很显然,我不能看到你的代码,所以我不能给你一个准确的例子。

因此,正如我在评论说,你可以模拟出一个类中的依赖伪造异步调用,使他们成为同步。采取以下类

public class RequiredQuestion extends EventDispatcher
{
    private var someAsynchronousObject : IAsynchronousObject;

    public function RequiredQuestion(someAsynchronousObject : IAsynchronousObject = null)
    {
        someAsynchronousObject = someAsynchronousObject || new AsynchronousObject();
        someAsynchronousObject.addEventListener(Event.COMPLETE, asyncCallComplete);
    }

    public function responseSelected(id : String, flag : Boolean) : void
    {
        //Will asynchronously fire the Event.COMPLETE event
        someAsynchronousObject.startAsynchrounsCall(); 
    }

    protected function asyncCallComplete(event : Event) : void
    {
        dispatchEvent(new ResponseChangedEvent(ResponseChangedEvent.RESPONSE_CHANGED));
    }
}

所以,在默认情况下,你使用的是您要使用,除非someAsynchronousObjec通过构造函数注入类的具体类。 AsycnhronousObject可能有它自己的单元测试,或者它在外部类,所以你真的不想要,或者需要测试其功能。你现在可以做的就是创建一个实现IAsynchronousObject模仿对象,可用于伪造其行为。使用ASMock框架的测试可能是这个样子:

public function testSomething(): void 
{
    var mockIAsycnhronousObject :  IAsynchronousObject =
        IAsynchronousObject(mockRepository.createStrict( IAsynchronousObject));

    SetupResult.forEventDispatcher(mockIAsycnhronousObject);
    SetupResult.forCall(mockIAsycnhronousObject.startAsynchronousCall())
        .dispatchEvent(new Event(Event.COMPLETE)); // all calls to the startAsynchronousCall method and dispatch the complete event everytime it's called.

    mockRepository.replayAll();

    var requiredQuestion : RequiredQuestion = new RequiredQuestion(mockIAsycnhronousObject);

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

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

    assertEquals(2, callCount);

    mockRepository.verifyAll();
}

这仅仅是一个多么嘲讽可以帮助你的单元测试的例子。有信息的全部财富在那里的嘲讽,虽然它仍然是非常新的动作脚本(12月份发布)。 ASMock是基于.NET犀牛嘲笑因此搜索犀牛嘲笑,如果你需要帮助,应该抛出了很多成果。

的思维绝对是一个不同的方式,但一旦你进入它你会想知道你是如何在单元测试通过了,没有他们。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top