Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top