【发布时间】:2023-03-06 00:16:01
【问题描述】:
我正在使用garb gem 从 Google Analytics(分析)中提取一些基本统计数据,例如浏览量。一切正常,但我想不出测试 API 调用的最佳方法。这是我的 Analytics 课程的配对版本:
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 中的数据不会改变。
标签: ruby-on-rails testing rspec google-analytics bdd