Deferring Tests with Test::Unit in Rails

Now that we have that nice syntax for tests in Rails, I’m happy just using the baked-in Test::Unit stuff. Well… maybe I still need Mocha. But the other stuff like RSpec, test/spec, and Shoulda? Meh. The only thing missing from Test::Unit is an easy way to defer tests. That’s important. I’d been dropping “flunk” in tests to note that they weren’t implemented yet, but that can get confusing pretty quickly.

Luckily, there’s a quick and easy way to add “deferred” tests. Here’s how:

# test/test_helper.rb
class Test::Unit::TestCase

  def defer
    puts "nDeferred: #{caller[0]}"
  end

end

# test/functional/home_controller_test.rb
require 'test_helper'

class HomeControllerTest < ActionController::TestCase  
  test "should defer test" do
    defer; return;
  end    
end

This would produce output like so…

~/git/h8ter $ autotest
loading autotest/rails
/opt/local/bin/ruby -I.:lib:test -rtest/unit -e "%w[test/functional/home_controller_test.rb...
Loaded suite -e
Started
....................
Deferred: ./test/functional/home_controller_test.rb:6:in `test_should_defer_test'
............................................
Finished in 0.795139 seconds.

64 tests, 123 assertions, 0 failures, 0 errors

I thought I’d seen a commit from Koz that added a nice way to defer tests in Rails, but I can’t seem to find it. Please post a comment if you know what I’m talking about. In the meantime, here we are with a quick and dirty solution for your enjoyment.

Published by

Trevor Turk

A chess-playing machine of the late 18th century, promoted as an automaton but later proved a hoax.