Skip to content

Instantly share code, notes, and snippets.

@ejfinneran
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejfinneran/90694503baea1d5c1cb3 to your computer and use it in GitHub Desktop.
Save ejfinneran/90694503baea1d5c1cb3 to your computer and use it in GitHub Desktop.
gem 'minitest'
class Object
alias_method :unsafe_send, :send
alias_method :safe_send, :public_send
alias_method :send, :safe_send
end
require './safe_send'
require 'minitest/autorun'
class MyClass
def my_public_method
"Hello"
end
private
def my_private_method
"Get out of here!"
end
end
describe MyClass do
it "should allow me to call #my_public_method" do
MyClass.new.my_public_method.must_equal "Hello"
end
it "should not allow me to call #my_private_method" do
MyClass.new.my_public_method.must_equal "Hello"
end
it "should not allow me to call #my_private_method" do
assert_raises(NoMethodError) {
MyClass.new.my_private_method
}
end
it "should not allow me to call #my_private_method using send" do
assert_raises(NoMethodError) {
MyClass.new.send(:my_private_method)
}
end
end
@ejfinneran
Copy link
Author

Got this far and then minitest stopped working because it expected send to work with private methods. ಠ_ಠ

@ejfinneran
Copy link
Author

/Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest.rb:21:in `public_send': private method `attr_accessor' called for #<Class:Minitest> (NoMethodError)
    from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest.rb:21:in `<module:Minitest>'
    from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest.rb:9:in `<top (required)>'
    from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest/autorun.rb:8:in `require'
    from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest/autorun.rb:8:in `<top (required)>'
    from safe_send_test.rb:2:in `require'
    from safe_send_test.rb:2:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment