在Flex中设置功能测试

时间:2010-04-23 20:08:32

标签: flex functional-testing flexunit

我正在为加载外部配置文件的应用程序设置功能测试套件。现在,我正在使用flexunit的addAsync函数来加载它,然后再次测试内容是否指向存在且可以访问的服务。

这样做的问题在于拥有这种两个(或更多)阶段方法意味着我在一个测试的上下文中运行了所有测试,其中有几十个断言,这似乎是一种退化的方式使用框架,使bug更难找到。有没有办法像异步设置?是否有另一个测试框架可以更好地处理这个问题?

3 个答案:

答案 0 :(得分: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");
    }

当您在程序中访问舞台时,应将其替换为:

if(stage==null) stage=testStageRef.stage

答案 1 :(得分:0)

听起来您需要删除加载该外部文件的依赖项。几乎所有的Aysnchronous测试都可以通过使用mocking frameworks来删除。 ASMock是Flex的绝佳选择。它将允许您伪造URLoader对象并返回伪造的配置以运行您的测试。 Mocking将帮助您编写更好的单元测试,因为您可以模拟同步或异步的所有依赖项。

答案 2 :(得分:0)

假设您正在使用FlexUnit 4,可以从[BeforeClass]方法调用addAsync:

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:)