Anonymous functons with FireUnit
FireUnit makes it quick and easy to write unit tests in JavaScript, doing something like the following:
fireunit.ok(foo==bar, 'foo should equal bar');
But what if you need to do something a little more complex than a one liner? You can write and execute an anonymous function, like this:
fireunit.ok(function(){
var myList = $('ul');
var originalLen = myList.children('li').length;
fireunit.click( myList );
return $(myList).children() == originalLen + 1;
}(), 'This test should return true');Two things to note here:
- Make sure that you include the extra parentheses after the function definition; this executes the function straight away
- Make sure you return true or false from the function (or an expression that evaluates to true or false.)









[...] ‘Anonymous Functions with FireUnit‘, I looked at one quick way of doing some unit testing within FireUnit. Here’s a [...]