[rspec-users] Specing protected methods

Avdi Grimm avdi at avdi.org
Thu Oct 23 17:09:45 EDT 2008


On Thu, Oct 23, 2008 at 5:00 PM, Avdi Grimm <avdi at avdi.org> wrote:
> FWIW, I blogged my answer to this question recently:
> http://avdi.org/devblog/2008/10/21/testing-private-methods/

...and to be a little more concrete, at first glance the way I'd apply
that advice to your code example is to make a class that either
subclasses or wraps (via DelegateClass/SimpleDelegate) Array and adds
a #sub_total method, something like the following:

class Survey < ActiveRecord::Base
 has_many :participants
 ...
 def generate_reports
   ...
   sub_total = ReportCollection.new(participants.reports).sub_total
   ...
 end

  class ReportCollection < SimpleDelegator
    def sub_total
      sub_total = 0
      self.collect do |report|
        sub_total = sub_total + report.score
      end
     return sub_total
    end
  end

end

-- 
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com


More information about the rspec-users mailing list