The three virtues of excellent programmers are well-known - laziness, impatience, and hubris. Last night, I had an epiphany related to the first of these… and thus was born the laziness plugin.
You know how, when you find a bug in your app, you’re supposed to write a failing test before fixing it? Well, laziness makes this a bit easier by writing the test for you. If you’re running in development mode, laziness will add a test like the following to the bottom of your error page:
def test_get_sessions_new_should_not_raise_exception
assert_nothing_raised do
get :new, {}, {:return_to=>"/"}, {:notice=>"You cannot access that page"}, {}
end
end
The first step to fixing the bug, then, is to paste that into your functional test file and run it; if it passes, you’re most likely looking at a data issue (since the params, session, flash, and cookies are specified in the test). If it fails, you’ve got a starting point for debugging.
Laziness isn’t just helpful in development mode, though - it also comes into play in production. If you’ve installed the exception_notification plugin (and you really should have - if not, go ahead; I’ll wait), then laziness will add its generated test to the email you receive whenever your production app throws an unexpected exception.
If you can’t wait to be lazy, you can grab the plugin from Viget’s SVN repository (at http://svn.extendviget.com/lab/laziness/trunk) or GitHub. Patches are welcome through either mode (git slightly preferred).
[...] lazy. The lazy way to get better test coverage: Laziness catches errors in your Rails application and turns them into test [...]
Hi, I installed your plugin in my rails app. When I tried to start mongrel server, it gave me error related to ExceptionNotifier. Is it compulsory to have it?
– Anil
Ah, sorry! If I had to say, I’d rate the plugin as being in early-alpha, so there are lots of bugs left to work out. I’m hoping to have any update for this later today.
PATCH (not in svn yet, in git?)
vendor/plugins/laziness/lib/laziness.rb
begin
module ExceptionNotifier
::ExceptionNotifier.sections
arg at markup
module Laziness
begin
module ExceptionNotifier
::ExceptionNotifier.sections << ‘laziness’
end
rescue
end
Thanks for the patch! I’d actually already updated the git version (with a different fix), but I forgot to do the same for the SVN copy. It’s updated now, and I’m hoping to find a better process for keeping them in sync in the future.
[...] 24, 2008 in Rails Tags: Rails, Ruby, Test Today i found a small new plugin, that offers a simple way to convert bugs to a [...]
How hard would it be to port this to rspec?
Actually, it already works with RSpec - if you install it on a project that has specs, you’ll get specs out from it instead of tests.
slick little tool