gpt4 book ai didi

ruby-on-rails - 如何使用 Rspec 测试 google analytics (garb) API?

转载 作者:行者123 更新时间:2023-11-28 20:12:44 26 4
gpt4 key购买 nike

我正在使用 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,也很难设定预期。

我想我想要模拟课?但我以前从未使用过这种模式。任何帮助都会很棒,即使只是一些让我走上正确道路的链接。

最佳答案

Fakeweb是一个很好的起点。它可以将您的 SUT 与网络隔离开来,这样慢速连接就不会影响您的测试。

如果不进一步了解 Garb,就很难知道还能说些什么。显然,您需要知道要从 API 发送和接收的数据的格式,以便您可以制作适当的模拟/ stub 。

关于ruby-on-rails - 如何使用 Rspec 测试 google analytics (garb) API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4832106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com