I’m developing a web app right now and as some of you might know there is a very popular javascript command which is used for debugging. It’s invoked via console.log() and takes anything as an argument. If you use Firefox with Firebug or Safari 4 the value you pass to console.log will be printed and introspected via a debug window.
The problem is that I forget on a regular basis to remove those console.log statements. I wrote a very quick and dirty perl script which searches files with an .js extension for console.log. I put that script in the t/ folder and named it forbidden_words.t.
It works great so far and my test suite fails as soon as there is still a console.log around. I plan to extend this script to something like Test::ForbiddenWords or something where you can specify file extensions and strings (or regexes).
I couldn’t find anything like this on the CPAN, so give me a comment if you like the idea or know something better.
I recently had to test my model which has a method which depends on DateTime->now. The problem with testing is, that the returned value will change depending on the current time.
I could have copied the logic from the model’s method but that would have made the test obsolete. Instead I choose to mock the current time and looked for a proper module on CPAN. And was lucky: Test::MockTime does just what I want:
use Test::MockTime qw(set_absolute_time); set_absolute_time('2009-04-24T00:00:00Z');
And that’s it!
But make shure you load modules like DateTime after Test::MockTime!