Writing Ruby? 👍 Using RSpec? 👍 Feeling naughty? 😱 Want to test some private methods? 😲
# Gemfile
gem 'rspec-context-private'
# example.rb
class Example
private def foo
'bar'
end
end
# example_spec.rb
describe Example do
describe 'some private methods', :private do
it 'does not raise an error' do
expect{subject.foo}.not_to raise_error(NoMethodError)
end
end
end
If you're using some flavor of TDD, you might want to test some private methods that you develop along the way. Maybe you'll delete them later? Or maybe you're a crazy rebel 😝 and you'll keep the tests around and make your code difficult to refactor and disappoint the Internet!
Whatever your reason - this gem makes it more pleasant to test private methods.
Written by @barelyknown.
This gem uses RSpec's shared context feature.
The idea for the gem came from this old blog post by Jay Fielder.