rubyruby-on-rails-3modulerspec-rails

How to test private helper(module) method in rspec


I have a helper module named "AppHelper" and private method "sum" which I want to test using rspec.

For example:

module AppHelper
 private
 def sum(a,b)
   puts a+b
 end
end

Solution

  • ideally private methods can not test directly, it should be called from public method but here is a hack

    create a dummy class and access private method using .send(:private_method, args)

    example

    obj = Class.new { extend AppHelper } obj.send(:sum, 1,2)