在 Flex 中设置功能测试

发布于 2024-08-30 05:34:53 字数 217 浏览 12 评论 0原文

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

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

I'm setting up a functional test suite for an application that loads an external configuration file. Right now, I'm using flexunit's addAsync function to load it and then again to test if the contents point to services that exist and can be accessed.

The trouble with this is that having this kind of two (or more) stage method means that I'm running all of my tests in the context of one test with dozens of asserts, which seems like a kind of degenerate way to use the framework, and makes bugs harder to find. Is there a way to have something like an asynchronous setup? Is there another testing framework that handles this better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兮子 2024-09-06 05:34:53

这很简单,但我花了两天时间才弄清楚。
解决方案:

首先您需要在某处创建一个静态变量。

 public static var stage:Stage

有一个由flexunit框架创建的FlexUnitApplication.as,在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");
    }

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

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

It is pretty easy, but took me 2 days to figure it out.
The solution:

First you need to create a static var somewhere.

 public static var stage:Stage

There is a FlexUnitApplication.as created by the flexunit framework, and at the onCreationComplete() function, you can set the stage to the static reference created previously:

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");
    }

and when you would access the stage in the program, you should replace it to:

if(stage==null) stage=testStageRef.stage
想念有你 2024-09-06 05:34:53

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

Assuming you're using FlexUnit 4, addAsync can be called from a [BeforeClass] method:

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");
    }
}

Having said that, testing code that accesses a file is really an integration test. I'm also hardly in a position to argue against using ASMock :)

厌味 2024-09-06 05:34:53

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

Sounds like you need to remove the dependency of loading that external file. Pretty much all Aysnchronous tests can be removed through the use of a mocking frameworks. ASMock is an awesome choice for Flex. It will allow you to fake the URLoader object and return faked configurations to run your tests against. Mocking will help with you write much better unit tests as you can mock all dependencies synchronous or asynchronous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文