Sunday, July 26, 2009

unit testing private methods in ruby

don't u just love this???


class Class
def publicize_methods
saved_private_instance_methods = self.private_instance_methods
self.class_eval { public *saved_private_instance_methods }
yield
self.class_eval { private *saved_private_instance_methods }
end
end

and to test the private method blah of a class Foo


class TestFoo &Test::Unit::TestCase
def test_blah
Foo.publicize_methods do
assert_not_nil Foo.blah
end
end
end


and thats it... :)