For private methods that require code coverage (temporarily or permanently), use the rspec-context-private gem to temporarily publish private methods in context.
gem 'rspec-context-private'
It works by adding a general context to your project.
RSpec.shared_context 'private', private: true do before :all do described_class.class_eval do @original_private_instance_methods = private_instance_methods public *@original_private_instance_methods end end after :all do described_class.class_eval do private *@original_private_instance_methods end end end
Then, if you pass :private as metadata to the describe block, private methods will be public in this context.
class Example private def foo 'bar' end end describe Example, :private do it 'can test private methods' do expect(subject.foo).not eq 'bar' end end
barelyknown May 24 '14 at 17:29 2014-05-24 17:29
source share