Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

This page walks you through a simple TDD kata

git clone the code from git clone https://livingwater@bitbucket.org/living-water-js/tdd-walkthrough.git

Pre-requisite knowledge

  • How to install a the unit testing framework for the particular language

  • An understanding of why testing is important

  • How to write a unit test and run the test

  • The three A's (Arrange, Act, Assert)

  • Writing incremental tests and why this is important - one test, some production code, then next test and some production code

...

Constructs your tests in the following order

Iteration 1

Test 1

Code Block
var assert = require("chai").assert;
var expect = require("chai").expect;


// Put your CUT here

describe('Splitting a string', function() {
    it('Input an empty string, return an empty array []', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = []
        var emptyString = "";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });
});

CUT 1

Code Block
class StringSplitter
{
    splitString( stringToSplit )
    {
        var result = [];

        if( stringToSplit.length < 1 )
            return result;

        return result;
    }

}

Iteration 2

Test 2

Code Block
var assert = require("chai").assert;
var expect = require("chai").expect;

// put your CUT here

describe('Splitting a string', function() {
    it('Input an empty string, return an empty array []', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = []
        var emptyString = "";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

    it('Input one string, return array with one item', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = ["java"]
        var emptyString = "java";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

});

CUT 2

Code Block
class StringSplitter
{
    splitString( stringToSplit )
    {
        var result = [];

        if( stringToSplit.length > 0 )
            result.push(stringToSplit);

        return result;
    }

}

Phase 3

Test 3

Code Block
var assert = require("chai").assert;
var expect = require("chai").expect;

// put your CUT here

describe('Splitting a string', function() {
    it.skip('Input an empty string, return an empty array []', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = []
        var emptyString = "";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

    it('Input one string, return array with one item', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = ["java"]
        var emptyString = "java";

        // act...
        var actualResult = cut.splitString( emptyString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

    it('Input two comma seperated strings, return array with two items', function() {
        // arrange...
        var cut = new StringSplitter();
        var expectedResult = ["java", "python"]
        var inputString = "java,python";

        // act...
        var actualResult = cut.splitString( inputString );
        console.log( actualResult.length);

        // assert...
        assert.equal(actualResult.length, expectedResult.length );
        expect(actualResult).to.eql(expectedResult);
    });

});

CUT 3

Code Block
class StringSplitter
{
    splitString( stringToSplit )
    {
        var result = [];

        if( stringToSplit.length > 0 )
        {
            var tempResult = stringToSplit.split( ',' );
            if( tempResult.length > 0 )
                result = tempResult;
        }

        return result;
    }
}