我使用garb gem從Google Analytics中提取一些基本統計信息,例如綜合瀏覽量。一切工作正常,但我無法弄清楚測試我的API調用的最佳方式。以下是我的Google Analytics類的配對版本:如何使用Rspec測試Google Analytics(Garb)API?
class Analytics
extend Garb::Model
metrics :pageviews
dimensions :page_path
Username = 'username'
Password = 'password'
WebPropertyId = 'XX-XXXXXXX-X'
# Start a session with google analytics.
#
Garb::Session.login(Username, Password)
# Find the correct web property.
#
Property = Garb::Management::Profile.all.detect {|p| p.web_property_id == WebPropertyId}
# Returns the nubmer of pageviews for a given page.
#
def self.pageviews(path)
Analytics.results(Property, :filters => {:page_path.eql => path}).first.pageviews.to_i
end
# ... a bunch of other methods to pull stats from google analytics ...
end
非常簡單。但除了確保常數已經確定之外,我還沒有能夠寫出有效的測試。測試這種類似的最好方法是什麼?這裏有一些問題:
- 我不想在我的測試中實際擊中API。這很慢,需要互聯網連接。
- 統計信息顯然會隨時變化,即使在測試時擊中API,也很難設置期望值。
我想我想要一個模擬課程?但我從來沒有使用過這種模式。任何幫助都會很棒,甚至只是一些鏈接讓我走上正確的道路。
你可以嘗試對幾個固定的時間段進行測試;舊GA中的數據不會改變。 – Yahel 2011-01-28 19:34:27