TDD with Jasmine

By | November 22, 2013

Today I was asked if I could make the definition files that we must send to the Swedish tax agency. This files have a specific format that we can get our description on from there website.

I though this was a perfect situation to test TDD with JavaScript using Jasmine.

I began to download the required files from the Jasmines website. All documentation are on that site as well so I began with the mandatory hello test (as mentioned in Goodbye Hello World).

describe("Hello test", function() {
    it("says hello", function() {
        expect(helloTest()).toEqual("Hello test!");
    });
});

Opening the SpecRunner.html that comes with jasmine gives the following expected result
mandatoryFailingTest

So to get it to pass, we simply creates the following method in our source js file:

function helloTest() {
    return "Hello test!";
}

Now we got it up and running, rerunning the tests is as simple as pressing F5 in the browser. So it is time to start implementing the behaviours. The implementation will be done in Swedish in this case since the domain is in Swedish.

I started with the INFO.SRU and defined each object in that and what it will generate. Then we do the same with BLANKETTER.SRU. By driven the development in this way at the end we had a blankett class and a fil class that can contain multiple blankett objects.

So I started to defining the INFO.SRU component with the code below. I did not define all at once but with one failing test at the time and then correcting it in the implementation.

describe("Databeskrivning", function() {
    var databeskrivning = new Databeskrivning();
    
    it("Ska ha en start", function() {
        expect(databeskrivning.databeskrivningStart()).toEqual("#DATABESKRIVNING_START");
    });

    it("Ska ha ett slut", function() {
        expect(databeskrivning.databeskrivningSlut()).toEqual("#DATABESKRIVNING_SLUT");
    });

    it("Ska omsluta innehållet", function() {
        var innehåll = databeskrivning.generera();
        expect(innehåll[0]).toEqual("#DATABESKRIVNING_START");
        expect(innehåll[innehåll.length - 1]).toEqual("#DATABESKRIVNING_SLUT");
    });

    describe("Generering av obligatoriska uppgifter", function() {
        it("Ska ha dagens datum och tid i rätt format", function() {
            expect(databeskrivning.skapad()).toMatch(/#SKAPAD \d\d\d\d\d\d\d\d \d\d\d\d\d\d/);
        });

        it("Ska ha produkt som SRU", function() {
            expect(databeskrivning.produkt()).toEqual("#PRODUKT SRU");
        });

        it("Ska ha program som SRU-Maker", function() {
            expect(databeskrivning.program()).toEqual("#PROGRAM SRU-Maker");
        });

        it("Ska ha filnamn som BLANKETTER.SRU", function() {
            expect(databeskrivning.filnamn()).toEqual("#FILNAMN BLANKETTER.SRU");
        });
    });
});

After implementing this I rerun the test and got a green result =).
allTestPasses

I continued in the same way with the rest of the system. Even that it may have felt slower then just hacking it directly without testing I caught spelling errors and also drew the design. So when I started my reference implementation it was already god to go and most of my problems was with the html and JavaScrip code to handle the UI layer.

So after some implementation of the reference implementation I got the following result.
referenceIplementation

The final code can be download from github.

I also published a live demo, in swedish, that can be found at http://aidium.se/demo/srumaker

90 thoughts on “TDD with Jasmine

  1. AvatarLars

    Vilken fantastisk tjänst! Hjälte!

    Tack så mycket, det fungerade helt perfekt.

    Reply

Leave a Reply

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