我們一直在使用Flex約6個月在這裏工作,我發現我的涉及的自定義組件FlexUnit測試的第一批將傾向於遵循這種模式:FlexUnit組件測試模式:使用addAsync還是手動初始化?
import mx.core.Application;
import mx.events.FlexEvent;
import flexunit.framework.TestCase;
public class CustomComponentTest extends TestCase {
private var component:CustomComponent;
public function testSomeAspect() : void {
component = new CustomComponent();
// set some properties...
component.addEventListener(FlexEvent.CREATION_COMPLETE,
addAsync(verifySomeAspect, 5000));
component.height = 0;
component.width = 0;
Application.application.addChild(component);
}
public function verifySomeAspect(event:FlexEvent) : void {
// Assert some things about component...
}
override public function tearDown() : void {
try {
if (component) {
Application.application.removeChild(component);
component = null;
}
} catch (e:Error) {
// ok to ignore
}
}
基本上,你需要確保在組件,然後才能可靠地驗證這事,並在Flex中發生這種情況已被添加到顯示列表之後,非同步完全初始化。所以你需要設置一個回調(使用FlexUnit的addAsync函數)以在發生這種情況時得到通知。
最近我一直在剛剛手動調用的運行時間將在必要的地方打電話給你的方法,所以現在我的測試往往看起來更像是這樣的:
import flexunit.framework.TestCase;
public class CustomComponentTest extends TestCase {
public function testSomeAspect() : void {
var component:CustomComponent = new CustomComponent();
component.initialize();
// set some properties...
component.validateProperties();
// Assert some things about component...
}
這是更容易跟蹤,但它有點感覺就像我在某種程度上作弊一樣。第一種情況下被猛地到當前應用程序(這將是單元測試轉輪殼應用程序),而後者是不是一個「真實」的環境。
我不知道別人怎麼會處理這樣的情況呢?