我々は仕事でここに約6ヶ月間のFlexを使用してきた、と私はカスタムコンポーネントを含む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...
}
これは従うことがはるかに簡単ですしかし、ちょっと私はちょっと騙されているように感じます。最初のケースは、現在のアプリケーション(これは単体テストランナーシェルアプリケーション)にスラミングしており、後者は「本当の」環境ではありません。
このような状況に他の人がどのように対処するのだろうと思いましたか?