Skip to content

Instantly share code, notes, and snippets.

@dv
Last active January 23, 2017 12:11
Show Gist options
  • Save dv/7655ada21edadb04dd4eedeb660570cd to your computer and use it in GitHub Desktop.
Save dv/7655ada21edadb04dd4eedeb660570cd to your computer and use it in GitHub Desktop.
When you really want to test a private method, you can use this simple helper to temporarily expose it for easier access during specs.
require "spec_helper"
RSpec.describe MyClass do
describe "#some_private_method" do
expose_method MyClass, :some_private_method
it "works" do
MyClass.new.some_private_method
end
end
end
# spec/support/expose_private_methods.rb
module ExposePrivateMethods
# Note: does not work on protected methods
def expose_method(klass, *methods)
before { klass.send(:public, *methods) }
after { klass.send(:private, *methods) }
end
end
RSpec.configure do |config|
config.extend ExposePrivateMethods
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment