Question

It seems that most XUnit testing frameworks provide assertions for the times when you want to assert that a given operation will thrown an exception (or an Error in AS3 parlance.) Is there some "standard" way of doing this that I am overlooking, which would explain the absence of an assertError() assertion included with FlexUnit?

I know HOW to implement such a thing, and I will probably add it to my FlexUnit (go open source!), but it seems like such a glaring omission that I'm left wondering if I'm just doing it wrong.

Anyone have thoughts on this?

Was it helpful?

Solution

Edit 05/02/2010: I'd now recommend using FlexUnit 4. It uses an extensible metdata system, supports expected exceptions, and also supports running in an integration server environment without the use of AIR.

Edit: You should have a look at fluint, which was built by people who had enough of FlexUnit and it's limitations. It might have some of these types of assertions built in.

I totally agree. In fact, FlexUnit is missing several useful methods (assertEvent, assertArrayEquals, etc). I know you said you know how to implement it, but feel free to use mine:

public static function assertError(message : String, func : Function, errorClass : Class = null, errorMessage : String = null, errorCodes : Array = null) : Error 
{
    _assertionsMade++;

    if (errorClass == null) errorClass = Error;

    try
    {
        func();
    }
    catch(ex : Error)
    {
        if (!(ex is errorClass))
        {
            fail("Expected error of type '" + getQualifiedClassName(errorClass) + "' but was '" + getQualifiedClassName(ex) + "'");
        }

        if (errorMessage != null && ex.message != errorMessage)
        {
            fail("Expected error with message '" + errorMessage + "' but was '" + ex.message + "'");
        }

        if (errorCodes != null && errorCodes.indexOf(ex.errorID) == -1)
        {
            fail("Expected error with errorID '" + errorCodes.join(" or ") + "' but was '" + ex.errorID + "'");
        }

        return ex;
    }

    if (message == null)
    {
        message = "Expected error of type '" + getQualifiedClassName(errorClass) + "' but none was thrown"
    }

    fail(message);

    return null;
}

OTHER TIPS

FlexUnit 4 mates well with hamcrest-as3. hamcrest has error assertion matchers

You may want to consider using this assertion tool.

It does not replace the xxxunit framework, just facilitate the assertions you make, make them more English and less code.

https://github.com/osher/should.as

var p:Person = new Person();

//assume a method p.sayHi()
p.sayHi.should().throwError('name is not set');

p.name = "Radagast";
p.sayHi.should().not.throwError();

Have fun :)

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