Actionscript3 Tdd Introduction Terminology

Title: Actionscript3 TDD: Introduction / Terminology Date: 2009-11-17 18:37 Author: eamonnfaherty Category: testing Tags: Actionscript, tdd Slug: actionscript3-tdd-introduction-terminology

I have been posting about design patterns recently and I am realizing that it would be useful to post about Test Driven Development too.

As a gentle introduction I am going to go through some of the vocabulary.

Test Driven Development is the process; Red, Green, Refactor. That is the easy way to remember it. Below is your workflow:

1) Write a unit test. A unit test is a method that tests another method. The test calls the method that you are testing and verifies that the response is as expected. Your unit test should always use the same inputs and should always expect the same response. The class containing the method you are testing is called your subject under test (SUT).

public function testAddingTwoNumbers():void {
    var result:int = sut.add(2,3);
    assertEquals(5, result);

2) Run your unit test. It should fail. This feels kind of silly but this is the best (easiest) way to check that the test is being run. This is the RED stage.

3) Write some source code that makes the test pass. This should simple literal values. Run your test and it should pass. This is the GREEN stage. This also feels silly but it proves your test passes before you add the complex logic in there.

public function add(valueOne:int, valueTwo:int):int {
    return 5;

4) This is now where you refactor. You take steps to make your design better. It is up to you how far to take the refactoring. In this example I would change the method to the following:

public function add(valueOne:int, valueTwo:int):int {
    return valueOne + valueTwo;

The example above was trivial. The add method does not use any other objects. If it did the correct name for those objects would be a collaborator. When you are TDD’ing you should replace collaborators with simple objects that return simple values. These are referred to as test doubles, after the term stunt double. You can use patterns such as the IOC pattern and Factory patterns to make it easier to replace collaborators with test doubles. That is coming in post 2!

Published: November 17 2009

  • category:
  • tags:
blog comments powered by Disqus