Topic: How would you spec this?

This code uses gem called garb to fetch data via google analytics data export api. All it actually does is setting up connection, search conditions and fetching results using garb api.

How would you spec this? All I can think about is just creating a couple of mocks for garb classes with expectations and calling VisitStat.fetch. Any better ideas?

class VisitStat
  def self.fetch(partner, date)
    VisitsReport.results(profile,
                         :start_date => date.at_beginning_of_month,
                         :end_date => date.at_end_of_month) do
      filters do
        eql(:campaign, partner.id.to_s)
      end
    end
  end

  private

  def self.profile
    conf = APP_CONFIG['google_analytics']
    Garb::Session.login conf['username'], conf['password']
    Garb::Profile.first conf['profile_name']
  end
end
require 'garb'

class VisitsReport
  extend Garb::Resource

  metrics :visits, :pageviews, :time_on_site, :new_visits, :bounces
  dimensions :source, :medium, :keyword, :ad_content
end

Re: How would you spec this?

Yes, I would also mock Garb calls.

If you want to make sure Garb behaves as you want when retrieving data from the google analytics data export api, you could use something like webmock or fakeweb and prepare some fixtures files containing google analytics data export api results. But that would rather spec a subset of Garb that you own code, so I'm not sure that's worth doing it.

Not directly related to your question: you might want to make sure that your code catches correctly deals with API failures, see http://rubyconf2008.confreaks.com/codin … ilure.html

Re: How would you spec this?

Thanks, Flo23!

Thats a great link smile