John McCollum

A (slightly) better way of working with FireUnit

In ‘Anonymous Functions with FireUnit‘, I looked at one quick way of doing some  unit testing within FireUnit. Here’s a slightly more robust pattern. In a seperate javascript file, you could do something like this:


var tests = {
    testOk: function(){
        fireunit.ok(1==1, 'This test will pass');
    },
    testCompare: function(){
        var numChildren = $('#myDiv').children('p');
        fireunit.compare(numChildren, 4, '#myDiv should have 4 child paragraphs');
    }
};

main = function(){
    if(typeof fireunit === 'object'){
        for(var test in tests){
            tests[test]();
        }
        fireunit.testDone();
    }
}();

Now, any test functions you add to the tests object will be run when the main function is called (in this case, straight away.)

Simply include this JavaScript file when you want to run your tests, and comment it out or remove it in production.

Posted in javascript, web development by admin at April 1st, 2010.
Tags:

Leave a Reply

This site is using OpenAvatar based on
Twitter Reddit Flickr LinkedIn Stack Overflow Github Email Ne RSS