Created
July 11, 2012 11:42
-
-
Save bonyiii/3089831 to your computer and use it in GitHub Desktop.
publicize private methods block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://stackoverflow.com/questions/267237/whats-the-best-way-to-unit-test-protected-private-methods-in-ruby | |
class Class | |
def publicize_methods | |
saved_private_instance_methods = self.private_instance_methods | |
self.class_eval { public *saved_private_instance_methods } | |
begin | |
yield | |
ensure | |
self.class_eval { private *saved_private_instance_methods } | |
end | |
end | |
end | |
MyClass.publicize_methods do | |
assert_equal 10, MyClass.new.secret_private_method | |
end | |
# or use send | |
class Secure | |
private | |
def yo | |
puts "nice" | |
end | |
end | |
s = Secure.new | |
s.nice | |
# NoMethodError: private method `yo' called for | |
s.send(:yo) | |
# nice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment