2010-04-23 70 views
1

我正在爲加載外部配置文件的應用程序設置功能測試套件。現在,我使用flexunit的addAsync函數來加載它,然後再次測試內容是否指向存在且可訪問的服務。在Flex中設置功能測試

這樣做的麻煩是擁有這種兩種(或更多)階段方法意味着我在一次測試的上下文中運行了所有的測試,其中有幾十個斷言,這似乎是一種退化的方式使用該框架,並使得難以找到錯誤。有沒有辦法像異步設置?有另一個測試框架能夠更好地處理這個問題嗎

回答

0

假設你正在使用的FlexUnit 4,addAsync可以從[BeforeClass]方法來調用:

public class TestFixture 
{ 
    [BeforeClass] 
    public static function fixtureSetup() : void 
    { 
     // This static method will be called once for all the tests 
     // You can also use addAsync in here if your setup is asynchronous 
     // Any shared state should be stored in static members 
    } 

    [Test] 
    public function particular_value_is_configured() : void 
    { 
     // Shared state can be accessed from any test 
     Assert.assertEquals(staticMember.particularValue, "value"); 
    } 
} 

說了這麼多,測試訪問一個文件中的代碼確實是一個集成測試。我也很難與ASMock爭辯:)

0

聽起來像你需要刪除加載該外部文件的依賴。幾乎所有異步測試都可以通過使用mocking frameworks來刪除。 ASMock是Flex的絕佳選擇。它將允許你僞造URLoader對象並返回僞造的配置來運行你的測試。模擬會幫助你編寫更好的單元測試,因爲你可以模擬所有的依賴同步或異步。

+0

我在單元測試中嘲笑HttpRequest, e應用程序正在運行。如果我基於罐裝測試而不是實際的配置文件,這不是非常有用。 – 2010-04-24 02:18:41

1

這很容易,但花了我2天的時間才弄明白。 解決方案:

首先你需要在某處創建一個靜態var。

public static var stage:Stage 

有是在FlexUnit框架創建一個FlexUnitApplication.as,並在onCreationComplete()函數,你可以設置舞臺,先前創建的靜態參考:

private function onCreationComplete():void 
    { 
     var testRunner:FlexUnitTestRunnerUIAS=new FlexUnitTestRunnerUIAS(); 
     testRunner.portNumber=8765; 
     this.addChild(testRunner); 
     testStageRef.stage=stage //***this is what I've added 
     testRunner.runWithFlexUnit4Runner(currentRunTestSuite(), "testsuitename"); 
    } 

,當你將訪問程序中的舞臺,你應該將其替換爲: