Use __END__ for keeping code around in testing

Written . Tagged Ruby, Ruby on Rails.

If you’re a bad boy and sometimes write tests after the code you’re testing, here’s a quick tip:

It’s nice to be able to glance at the code being tested while writing the tests. When working e.g. with Rails, the tests and the code are typically in different files.

Copy the code being tested. Paste it at the bottom of the test file, under an END line, and it will in effect be commented out (it actually goes into the DATA global constant).

So while writing a test, your buffer may now look like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require File.dirname(__FILE__) + '/../test_helper'
class MyTest < Test::Unit::TestCase

  def test_something
    # TODO
  end

end

__END__

# This stuff is in effect commented out.
def something(input)
  "hah!"
end

I remove the END and everything after it once the test has been written.