Last active
January 23, 2017 12:11
-
-
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.
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
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 |
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
# 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