Accessibility
Jason Peters

Jason Peters

International Game Technology

Paulo Caroli

Paulo Caroli

ThoughtWorks

Table of Contents

Created:
19 January 2009
Products:
Flash
Flex

Unit testing with mock objects in ActionScript 3.0

When you perform unit tests, it is not always possible to verify that an object does what you expect by examining that object directly. Instead, you may need to examine the collaborations between objects. Mock object frameworks simplify behavior verification—the process of verifying the interaction between dependent components.

This article is intended for developers interested in adding behavior verification to their unit tests. It discusses the role of behavior verification in unit testing and introduce the basics of Mock4AS, a lightweight mock object framework for ActionScript 3.0.

Behavior verification's role in unit testing

In unit testing lingo, the object you are testing is called the System Under Test (SUT for short). Each object that the SUT depends on to fulfill its tasks is called a Depended On Component (DOC for short).

For example, consider an object that is used to generate a greeting message when a user logs on to your website. For localization purposes, this Greeting object uses a Translator object to translate "hello" into the desired language. You want to test the Greeting object, so it is the SUT. This object depends on the Translator to get the right message, so the Translator is the DOC.

To verify that the SUT collaborates with the DOC in the way you expect, you can replace the usual DOC with a test double, called a mock. You then tell the mock what it should expect from the SUT. Once those expectations are set, you exercise the SUT by calling methods on it. In turn, the SUT calls methods on the mock. After you are finished exercising the SUT, you query the mock to see if it was used in the way you expected.

Mock objects frameworks provide the necessary mechanisms to set behavior, record incoming method calls, and verify that the methods were called as expected.

Requirements

In order to make the most of this article, you need the following software and files:

Flex Builder 3

Mock4AS

FlexUnit

Sample files:

Prerequisite knowledge

You should have a good understanding of ActionScript 3.0 and object-oriented principles, as well as some knowledge of unit testing.

About the authors

Jason Peters is a lead Flash/Flex engineer for International Game Technology. He has over eight years of experience designing and developing a variety of applications such as game engines, client content management systems, and e-learning frameworks. Jason also trains developers in refactoring, OOP, and agile development techniques.

Paulo Caroli is an agile coach and senior developer for ThoughtWorks. His main expertise lies in agile development and object-oriented techniques, utilizing a variety of technologies, in the e-commerce and telecommunication industry. With more than 13 years of experience in software development, Paulo has successfully performed a variety of roles, including developer, application architect, project manager, business analyst, and trainer. You can reach him at caroli.org.