I generally make a point of not unit testing private methods however in some circumstance I find it necessary to do so. One example of this is when a class method is to be documented in the API of a library, only for use when declaring a descendent class (the Rails validates_* macros could be private for example).
Testing of private instance methods has already been documented by Jay Fields, however testing private class methods requires a little more work:
class Class
def publicize_private_class_methods
saved_instance_methods = (class << self; private_instance_methods.sort; end)
saved_instance_methods.each { |method| public_class_method method }
yield self
ensure
saved_instance_methods.each { |method| private_class_method method }
end
end
This allows you to do the following:
class Foo
class << self
private
def foo
'bar'
end
end
end
Foo.publicize_private_class_methods do |klass|
assert_equal 'bar', klass.foo
end
The same would work for protected methods by changing all instances of the word ‘private’ with ‘protected’.
All this could probably be merged into the original method by Jay, however I will leave that as an exercise for the reader.
Why not just use class_eval to call the private methods?
MyClass.class_eval { private_class_method }
That will return whatever the method returns.
Amos,
That doesn’t work as private_class_method is used to make a method private, not call it. I wish it was that easy (maybe I’m missing something?).
In any case I would rather wrap the functionality up in a single method of Class for cleanliness.
Jamie
I didn’t mean private_class_method like the ruby method. Sorry. I meant a private class method. Maybe I should have put private_class_method_foo. Sorry for the confusion.
Aha, I see what you mean. Unfortunate method clash there :). Yes you could do that also.