-
-
Save mkhl/28127 to your computer and use it in GitHub Desktop.
Bypass encapsulation to test private methods (Ruby)
This file contains 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
# This allows you to be a good OOP citizen and honor encapsulation, but | |
# still make calls to private methods (for testing) by doing | |
# | |
# obj.bypass.private_thingie(arg1, arg2) | |
# | |
# Which is easier on the eye than | |
# | |
# obj.send(:private_thingie, arg1, arg2) | |
# | |
class Object | |
class Bypass | |
instance_methods.each do |m| | |
undef_method m unless m =~ /^__/ | |
end | |
def initialize(ref) | |
@ref = ref | |
end | |
def method_missing(sym, *args) | |
@ref.__send__(sym, *args) | |
end | |
end | |
def bypass | |
Bypass.new(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment