简体   繁体   English

如何使用Rspec测试Google Analytics(分析)API?

[英]How to test google analytics (garb) API with Rspec?

I'm using the garb gem to pull some basic stats, like pageviews, from Google Analytics. 我正在使用Garb gem从Google Analytics(分析)中提取一些基本统计信息,例如综合浏览量。 Everything's working correctly but I can't figure out the best way to test my API calls. 一切工作正常,但是我不知道测试API调用的最佳方法。 Here's a paired down version of my Analytics class: 这是我的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

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. 我不希望在测试中实际击中API。 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. 这些统计数据显然一直在变化,即使我在测试时确实点击了API,也很难设定期望值。

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. 任何帮助都将非常棒,甚至只有一些链接才能使我走上正确的道路。

Fakeweb is a good place to start. Fakeweb是一个不错的起点。 It can isolate your SUT from the network so that slow connections don't affect your tests. 它可以将您的SUT与网络隔离开,以便慢速连接不会影响您的测试。

It's hard to know what else to say without knowing more about Garb. 在不了解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. 显然,您需要知道要从API发送和接收的数据的格式,以便可以进行适当的模拟/存根。

I would suggest creating a testing interface that mimicks the actual calls to the google API. 我建议创建一个测试界面,以模仿对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. 我同意最好不要使用实际的API,因为这样做不会给您带来任何好处。 A call to the actual API might succeed one day and fail the next because the API owners change the response format. 由于API所有者更改了响应格式,因此对实际API的调用可能一天会成功,而第二天可能会失败。 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. 由于GA可能不会更改它的版本API,因此我认为创建一个可以在测试环境中使用的接口以进行更快的测试是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM