如何使用 Rspec 测试 google Analytics (garb) API?

发布于 2024-10-14 23:24:23 字数 1075 浏览 1 评论 0原文

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

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

I'm using the garb gem to pull some basic stats, like pageviews, from Google Analytics. Everything's working correctly but I can't figure out the best way to test my API calls. Here's a paired down version of my Analytics class:

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

Pretty simple. But beyond ensuring that the constants are set, I haven't been able to write effective tests. What's the best way to test something like this? Here are some of the problems:

  • I'd prefer not to actually hit the API in my tests. It's slow and requires an internet connection.
  • The stats obviously change all the time, making it difficult to set an expectation even if I do hit the API when testing.

I think I want a mock class? But I've never used that pattern before. Any help would be awesome, even just some links to get me on the right path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2024-10-21 23:24:23

Fakeweb 是一个很好的起点。它可以将您的 SUT 与网络隔离,以便缓慢的连接不会影响您的测试。

如果不了解更多关于 Garb 的信息,就很难知道还能说什么。显然,您需要知道从 API 发送和接收的数据的格式,以便您可以进行适当的模拟/存根。

Fakeweb is a good place to start. It can isolate your SUT from the network so that slow connections don't affect your tests.

It's hard to know what else to say without knowing more about Garb. Obviously you'll need to know the format of the data to be sent and received from the API, so you can make the appropriate mocks/stubs.

ら栖息 2024-10-21 23:24:23

我建议创建一个模拟对 google API 的实际调用的测试接口。另一种选择是使用模拟来创建示例数据。

我同意最好不要使用实际的 API,因为这不会给你带来任何好处。对实际 API 的调用可能有一天会成功,第二天就会失败,因为 API 所有者更改了响应格式。由于 GA 可能不会更改其版本化 API,因此我认为创建一个可在测试环境中使用的接口以加快测试速度是安全的。

I would suggest creating a testing interface that mimicks the actual calls to the google API. The other option would be to use mocks to create sample data.

I agree that it's best to not hit the actual API, since this does not gain you anything. A call to the actual API might succeed one day and fail the next because the API owners change the response format. Since GA probably won't change it's versioned API I think it's safe to create an interface that you can use in your test environments for faster testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文