Eventual Assertions in FlexUnit 4

After a conversation with a work colleague about EventuallyMatchers in Specs (a BDD Testing Framework for Scala). The basic idea behind Eventually Matchers is that you have a condition you with to assert against which won’t be met until some time in the future. For example, a Tween will take some time to complete and you want to check that the target MovieClip has reached a specific position. Using FlexUnit 4′s Async.delayCall() you can create a test which meets this condition, it would look something like this:

[Test (async)]
public function spriteTweensToTargetPosition() : void
{
	// This is the client class which is under test.
	const shape : MovingShape = new MovingShape();
 
	// This is where we expect the Shape to end up.
	const expectedPosition : Point = new Point(300, 200);
 
	// This function will be invoked when the Async.delayCall() time elapses.
	const assertionCallback : Function = function() : void {
 
		// Check to see if the shape is at the expected position.
		Assert.assertEquals("Shape is at the expected x position", expectedPosition.x, shape.x);
		Assert.assertEquals("Shape is at the expected y position", expectedPosition.y, shape.y);
 
	};
 
	// OK, let's get this shape moving.
	shape.tweenTo(expectedPosition);
 
	// Wait for 1000ms and then invoke the assertionCallback function.
	Async.delayCall(this, assertionCallback, 1000);
}

So, the above test passes, but according to the Test Runner it took 1.05 seconds to run – that’s not so good. The reason for this is because the Async.delayCall() will wait until the supplied delay value (1000) has elapsed before making the assertions defined in the assertionCallback. The EventuallyMatchers improve upon this situation by constantly polling the assertion until it is either eventually true (in which case the test passes) or the timeout arrives (in which case the test fails). Here’s the same testcase again but making use of my own bash at an EventuallyMatcher in ActionScript:

[Test (async)]
public function spriteTweensToTargetPosition() : void
{
	const shape : MovingShape = new MovingShape();
	const expectedPosition : Point = new Point(300, 200);
 
	shape.tweenTo(expectedPosition);
 
	assertEventuallyEquals(this, "Shape is at the expected x position", expectedPosition.x, function() : * { 
		return shape.x; 
	});
	assertEventuallyEquals(this, "Shape is at the expected y position", expectedPosition.y, function() : * { 
		return shape.y; 
	});
}

This testcase only takes 0.26 seconds to execute which is great, but legibility has suffered a fair bit due to the rather clunky way we declare closures in ActionScript. I’ve pushed this code to my FlexUnit branch on github; hopefully a cleaner syntax can be worked out, or someone else finds it useful :)

This entry was posted in ActionScript 3 and tagged . Bookmark the permalink.

One Response to Eventual Assertions in FlexUnit 4

  1. Sean Parsons says:

    This co-worker sounds awesome! :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">