I’ll be doing a talk at BRUG tomorrow evening (29th September 2010) on JavaScript testing covering some of the more awkward testing scenarios you may come across in JavaScript land. This months BRUG is at Beef Towers, hopefully we’ll see some new faces this month.
Programatically Simulating JavaScript Events in a Test Environment
Yesterday I was implementing a feature on tutorhub.com where I wanted to disable the sending of chat messages when the party you’re talking to goes offline. I ran into a problem while trying to write the tests for this feature and thought I’d share it in case someone else finds it useful.
When in a chat, messages are sent either using the send button or by pressing the enter key. Testing the correct behaviour on button press is straight forward. I’m using Jasmine and jsMocha for testing and jQuery for implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Our UI code raises events that our main application code listens to in order to send out the messages. Here I setup a mock in the before block saying the raise method should never be called with the ‘message_send’ parameter. Then in the test I insert some text into the text area and simulate a click event on the send button.
It became slightly more tricky when I came to test the enter key functionality, our implementation code looks something like this:
1 2 3 4 5 | |
In order to programatically simulate a press of the enter key I needed to pass an event object containing a which value of 13. After a bit of hunting around I found the jQuery.Event object, that allows the creation of events which can then be fired. The test for disabling the enter key looked like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Using this technique it should be possible to programatically simulate any key event in a test environment.
Introducing Scupper, the JavaScript Library for Easily Dealing With HTML Snippets in Test Suites
I came across a problem today while writing tests in JavaScript. The code I was testing required a snippet of HTML to work with. A user list needed to be reordered depending on their status. No problem I thought, I’ll create div to store an HTML snippet, then before my test I’ll duplicate and copy it into a test div.
1 2 3 4 5 6 7 8 | |
And the JavaScript to copy the element:
1
| |
Of course when working with id’s in HTML they have to be unique so the technique caused some of the other tests in the suite to fail, I needed to find another way to do this. One thing I hate is using jQuery to create more than a few dom elements as it gets complex very quickly and it’s not easy to see if the code is producing the desired HTML at a glance.
After taking a break I came up with a simple solution, the Scupper library was about to be written. I wanted to write snippets in HTML so I kept the snippet used in the first attempt. I then created a library that collected all of the snippets from the dom, storing them internally, before deleting them from the dom. This allowed the dom to be free from conflicting id’s and general pollution. The HTML snippet became:
1 2 3 4 5 6 7 8 9 10 | |
The containing div #user-list-snippet gives an element to latch onto in order to grab the contents inside. I created a method that sucks up all snippets inside a dom element and stores them:
1 2 3 4 5 6 7 8 | |
All that was needed is an easy way to pull them out an insert them into the dom:
1 2 3 4 5 6 7 8 9 10 11 | |
Calling insert_into() grabs the snippet HTML and inserts it into the specified dom element ready for the test to use it.
If you want to use Scupper the source is freely available on github.
Shoulda Macro Should_render_a_form_to
I’ve been writing a fair number of functional tests recently, one thing that kept cropping up was the need to check if a form had been rendered and that it is going to perform a particular action. Shoulda has a should_render_a_form macro, unfortunately it’s been depreciated and doesn’t do anything other than check a form element has been rendered in the view.
I decided to come up with my own macro that checks the specifics of a form element, enter should_render_a_form_to. This takes three arguments, a description, an options hash and a block that contains the expected url. You can use the macro as follows…
Check there is a form posting to the new_user_post_path:
1
| |
Check there is a form putting to the user_post_path and that the form has the id of ‘post_edit_form’:
1
| |
The macro code is available on github with test coverage. If you just want to cut and paste into your own macro’s file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
The macro checks both the forms action attribute as well as the hidden input rails uses to specify the method where necessary. I’ve also been playing with creating a macro to check for a form with specific fields such as should_render_a_form_with_fields. This is proving to be slightly more difficult than I originally anticipated and defining a nice interface to the method has been rather tricky.
Vlad the Deployer Hoptoad Integration
I’ve just had to setup Hoptoad for one of our apps that uses Vlad for deployment, the integration isn’t quite as easy as with Capistrano. I couldn’t find much information on how to integrate the two so I thought I’d share my solution.
The original Hoptoad task for use with Capistrano needed a little modification.
1 2 3 4 5 6 7 8 | |
fetch is a Capistrano method so needed to be removed, we can use the Vlad environment pattern for this. I also wanted to use the git information for the user instead of the system user, finally as far as I can tell the git commit SHA being deployed is not available in Vlad.
In the Vlad deployment script I added a Hoptoad task to replace the default Capistrano task provided by Hoptoad.
1 2 3 4 5 6 | |
Then added it as a dependency for the deploy task
1
| |
There are a couple of helper tasks I’ve added to get the git user and the SHA of the commit being deployed
1 2 3 4 5 6 7 | |
For the Love of Table Football, Why I Stayed Up for 48 Hours
Update: wuzlr.com is no longer a live site
What a weekend! it all started on Friday night, a feverish last minute planning session began on how we would implement “Wuzlr”. We’d bounced around some ideas earlier in the week and had a pretty good idea of what we wanted, whittling that down into a set of features we could implement in 48 hours was no easy task, there was so many good ideas and we didn’t have time to implement them all. At 1am it all began…
The Pitch
If your office is anything like ours things get pretty serious whenever a game of table football breaks out (especially when @theozaurus is playing). We’ve wanted a way to track who’s the best in the office for quite some time, finally we have just the thing, and so do you. Wuzlr is a table football league tracking application that lets you see performance over time with all sorts of fun and interesting facts and figures displayed.
Wuzlr (or wuzler if you use the correct non web 2.0 spelling) is the Austrian word for table football and just so happened to be the first domain we came across that’s still available. We also liked the hat tip to the ‘e’ dropping crowd, no, not you party people, I’m talking about flickr and the like.
Application Features
- Create leagues and record all your games,
Check out the Jiva office league - Compare yourself to other players,
Me Vs. Theo

- View your nemesis, best team mate, worst team mate and more,
my player page - League standings, games played per day, table bias, most dedicated players (who’s put the most time in)

Our Team
- Jamie Dyer @kernowsoul Developer for Jiva
- Theo Coushion @theozaurus Developer for Jiva
- Peter Coles @fatelvis Designer for Existem
Yes we know the site looks terrible in IE, who uses IE anyway?
My Computer Loves Autotest-fsevent
I’m a big fan of autotest for testing, unfortunately it does stress my poor MacBook Pro and makes the fan go berserk if running anything other than the most simple of test suites. This is due to autotest having to check each file in your project for changes.
No more will autotest stress out my mac, autotest-fsevent is a great gem that uses OS X’s FSEvent system to be notified when files have changed rather than having to constantly poll the filesystem. You need mac OS X 10.5 or later to take advantage of FSEvent.
The other nice thing autotest-fsevent does is take care of all the .autotest config options, I managed to delete my entire config file which I’ve been tweaking for as long as I can remember trying to get the perfect setup.
I can now run even the most demanding of test suites and my computer barely breaks a sweat. Thanks bitcetera, my computer ♥’s you.
DRYing Up Multiple User Contexts With Shoulda Macros
Today I’ve been writing tests for a legacy Rails application I inherited recently. The application has several user roles, each role having varying permissions. To deal with this nicely I setup shoulda macro’s to create contexts for each of the user roles, public user, standard user, admin user etc. then in my tests I could write…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
This is pretty standard practice now and something I picked up from looking at the code produced by the guys at Thought Bot. While working on the test suite it became apparent many of the methods behaved in the same way for multiple user roles. I wanted to come up with a way to run a group of tests under multiple user roles without having to duplicate any code. Shoulda macros to the rescue again! After creating another macro to deal with multiple contexts I can write my tests like this…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
And the shoulda macro code itself…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Running JSLint Through TextMate
UPDATE: I now use jsHint which is more configurable, there’s an excellent TextMate bundle available
I’ve been developing with JavaScript a lot recently and found JSLint to be a rather useful tool for picking up syntax errors. Being primarily a ruby developer I have a nasty habit of omitting semicolons from the end of lines.
I found an article on running JSLint through a TextMate command but found Ryan’s script didn’t quite work with the output from the version of JSLint I’m using so I thought I’d share my modifications.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
New Bars and Rubber for the Steed

A couple of weeks ago I had to replace the old tires on my Fillmore as I’d worn through the green ones that came with the bike. Took a bit of getting used to the orange but it’s definitely growing on me.
This last weekend I managed a puncture the inner tube while pulling the pump off which required a trip to Strada Cycles. I ended up replacing the bars and break callipers so it turned out to be a much more expensive trip than I was expecting. Just shy of £100 got me a new inner tube and I was back on the road again.