代码之家  ›  专栏  ›  技术社区  ›  Dan Monego

在flex中设置功能测试

  •  1
  • Dan Monego  · 技术社区  · 16 年前

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

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

    3 回复  |  直到 14 年前
        1
  •  0
  •   Richard Szalay    16 年前

    假设您使用的是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:)

        2
  •  1
  •   csomakk    14 年前

    这很容易,但我花了两天时间才弄明白。 解决方案:

    首先需要在某个地方创建一个静态var。

     public static var stage:Stage
    

    有一个flexunitapplication.as由flexunit框架创建,在onCreationComplete()函数中,可以将stage设置为先前创建的静态引用:

    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
    
        3
  •  0
  •   James Hay    16 年前

    听起来你需要删除加载外部文件的依赖关系。几乎所有的日常测试都可以通过使用 mocking frameworks . ASMock 对flex来说是个不错的选择。它将允许您伪造urloader对象,并返回用于运行测试的伪造配置。模拟将有助于编写更好的单元测试,因为您可以模拟所有同步或异步依赖项。

    推荐文章