views:

46

answers:

2

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?

A: 

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.

James Hay
I mock the HttpRequest in the unit tests, but I'd like to have a parallel set of tests that check if the live dependencies of the app are up and running. If I base those on a canned test rather than the actual configuration file, it isn't very useful.
Dan Monego
A: 

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

Richard Szalay