John McCollum

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:

  1. Make sure that you include the extra parentheses after the function definition; this executes the function straight away
  2. Make sure you  return true or false  from the function (or an expression that evaluates to true or false.)
Posted in javascript, web development by admin at March 22nd, 2010.
Tags: ,

One Response to “Anonymous functons with FireUnit”

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

Leave a Reply

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