2009-07-22 27 views
3

我在Flex應用程序中測試了一些事件調度代碼,使用FlexUnit的addAsync方法來測試事件是否被調度。目前爲止,我可以確保至少有一個事件被解僱。但是,我想要更詳細一些;我希望確保我所期待的一系列事件得到調度。有沒有一個有用的測試模式(或者,甚至是不同的測試框架 - 我很靈活!)來完成這個任務?Flex,Flexunit:如何測試事件分派兩次?

我試過這個代碼,但它似乎並沒有被調用第二次:

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); 
} 
+0

你幾乎可以肯定獲得通過ASMock(http://asmock.sourceforge.net/)嘲笑無論是在製作的requiredQuestion來電asynchrous擺脫addAsync的。一旦你擺脫了addAsync,一切都將同步運行。如果還沒有答案,會在明天上午(英國)提出。 J – 2009-07-22 20:22:37

回答

2

在迴應評論...

如果有什麼事件被直接發送 ? 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); 
} 
2

這將是怎樣一個類似的問題,可以使用來解決高層次例如嘲笑了無論是做異步調用的對象。顯然我看不到你的代碼,所以我不能給你一個確切的例子。

所以,正如我在評論中所說的,你可以模擬出一個類的依賴關係來僞造異步調用,以使它們變成同步的。採取下面的類

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(); 
} 

這僅僅是一個多麼嘲諷可以幫助你的單元測試的例子。儘管對於ActionScript(12月份發佈)來說還是很新的,但嘲笑的內容還是非常豐富。 ASMock基於.net Rhino mocks,所以如果你需要幫助,尋找Rhino mock應該會產生更多的結果。

絕對是一種不同的思維方式,但一旦你進入它,你會想知道如何在沒有它們的單元測試中得到。

+0

如果事件直接發送,該怎麼辦? responseSelected不會觸發組合對象上的異步事件,它只是直接分派RESPONSE_CHANGED事件本身。我沒有看到如何使用你的方法來嘲笑這種方法。請注意,我對模擬測試實踐是模糊不清的,所以我可能在這裏錯過了一個簡單的解決方案。 – 2009-07-23 15:37:49