lundi 22 juin 2015

Javascript Revealing Module Pattern and Unit Testing with QUnit

For my javascript I combine a mixture of the Revealing Module and Single Patterns.

The module pattern encloses it's logic only making specific objects public.

var Foo = function(){

    function DoesNameBeginWithA(name){    
       return name
              && name.length
               && name.slice(0,1).toLowerCase() == "a";
    }

    function Init(){
        console.log(DoesNameBeginWithA("Anne"));
        console.log(DoesNameBeginWithA("Ben"));
        console.log(DoesNameBeginWithA("Craig"));
    }

    return {
       Init: Init
    };

}();

Foo.Init();

Using this example I would like to unit test the DoesNameBeginWithA function.

This function isn't accessible outside of the Foo object and is therefore private.

I could of course expose this function as a property of Foo, but this defeats the point of making it private in the first place.

How can I use unit testing, such as QUnit, to test my private javascript functions?

Aucun commentaire:

Enregistrer un commentaire