Hi all,
It's always bothered me having code and unit tests in different places - keeping unit tests near the code they are testing *has* to be a good thing, right?
Given the above, I stumbled on the following idiom:
$ cat testable.rb
module Test; module Unit; class TestCase; end; end; end
module Testable
def test_case(&block)
Class.new(Test::Unit::TestCase).module_eval &block
end
end
$ cat builtin_tests.rb
require 'testable'
class MyWorkingClass
extend Testable
def foo(a,b)
return a+b
end
test_case {
def test_foo_null
assert_equal 2, MyWorkingClass.new.foo(1,1)
end
}
end
if __FILE__ == $0
puts MyWorkingClass.new.foo(1,1)
end
$ ruby builtin_tests.rb
2
$ testrb builtin_tests.rb
Loaded suite builtin_tests.rb
Started
.
Finished in 0.00095 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
$
Just thought I'd share Are there any drawbacks to this method that I haven't seen yet?
···
--
Alex