首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用RSpec对使用REST的方法进行单元测试?

如何用RSpec对使用REST的方法进行单元测试?
EN

Stack Overflow用户
提问于 2016-02-16 12:54:32
回答 4查看 1.1K关注 0票数 3

我正在从事一个从外部REST (从Facebook、Twitter或Instagram等社交网络)获取数据的项目。

我不确定我所做的是对是错,所以我需要一些指导。我不知道,当人们创建一个依赖于外部数据( REST或爬行数据)的应用程序时,他们如何使用它进行TDD。

,我的问题是:我试图对调用外部REST的方法进行TDD测试。这是对还是错?

  • 如果是正确的,我如何用RSpec测试它?有什么我能读到的指南或来源吗?
  • 如果是错的话,那我怎么查呢?如果我将API_VERSION更改为更高的版本,我如何才能知道逻辑仍然正常工作,并且所有必需的字段仍然存在?

例如,

我有这样的代码:

代码语言:javascript
运行
复制
API_VERSION = "v2.5"
FIELD_PAGE_GRAPH = %w(id name picture{url} likes cover is_community_page category link website has_added_app 
  talking_about_count username founded phone mission location is_published description can_post checkins company_overview
  general_info parking hours payment_options access_token
)

FIELD_STREAM_GRAPH = %w(id message story comments.summary(true) likes.summary(true).limit(500) from to link shares created_time
  updated_time type is_published attachments scheduled_publish_time application
)

def self.get_stat_facebook(page_id,access_token=nil)
  graph = Koala::Facebook::API.new(access_token)
  graph.get_objects(page_id.to_s,{:fields => FIELD_PAGE_GRAPH}, {:api_version => API_VERSION})
end

def self.get_feed_facebook(page_id,access_token=nil, options = {})
  options = options.with_indifferent_access
  retry_time = 0
  begin
    graph = Koala::Facebook::API.new(access_token)
    params = {:fields => FIELD_STREAM_GRAPH, :limit => 25}
    params.merge!({:since => options[:_since].to_i}) if options[:_since].present?
    params.merge!({:until => options[:_until].to_i}) if options[:_until].present?
    results = []
    loop do
      graph_response = graph.get_object(page_id.to_s+"/feed", params, {:api_version => API_VERSION})
      break if graph_response.blank?
      results = results+graph_response
      break if options[:_since].blank?
      params[:until] = graph_response.sort_by!{|result| result['created_time']}.first['created_time'].to_time.to_i-1
    end
  rescue Koala::Facebook::ServerError
    sleep 1
    retry_time += 1
    retry if retry_time <= 3
  end
  filter_owner_page(results, page_id)
end

那我就有了一个规范

代码语言:javascript
运行
复制
require 'spec_helper'

RSpec.describe SocialNetwork do
  context ".get_stat_facebook" do
    it "when access token is expired"
    it "when access token is not expired"
    it "when page id is not exist"
    it "when page id is exist"
  end

  context ".get_feed_facebook" do
    it "when access token is expired"
    it "when access token is not expired"
    it "when page id is not exist"
    it "when page id is exist"
    it "data contain id field"
    it "data contain message field"
    it "data contain attachment field"
  end
end
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-02-16 16:29:30

是的,测试访问外部服务是合适的,但您可以通过几种方式将其对测试套件的影响降到最低。

我将对此代码进行如下测试:

  • 注册一个Facebook测试用户
  • 编写RSpec特性规范或Cucumber场景,使用测试用户测试整个堆栈中使用Facebook的整个功能。使用VCR宝石记录Facebook的响应,这样远程调用不会减慢测试的速度。
  • 为RSpec编写SocialNetwork规范(单元测试)。
代码语言:javascript
运行
复制
- In one or a few unit tests per different call to Facebook, let them hit Facebook using the test user and, again, use VCR to keep them fast. Optionally, let one or a few of them hit Facebook all the time so that you know if Facebook's API changes.
- In the rest of your specs of `SocialNetwork`, ones that just test variations of calls that you already tested, stub out Koala.

很难确切地解释哪些测试应该在Facebook上进行,哪些测试应该使用存根而不让它们出现在我们面前。看看进展如何,如果你需要更多的建议,可以再发一次。

票数 2
EN

Stack Overflow用户

发布于 2016-02-16 16:38:16

TDD用于测试作为一个单元的方法。来自外部的数据可以是嘲弄,因此您可以涵盖每一种场景。有点像

代码语言:javascript
运行
复制
graph = double()
allow(graph).to receive(:get_object).and_return(data)

--

我也会改变

context ".get_stat_facebook" do

describe ".get_stat_facebook" do

并使用上下文来描述要测试的场景。它将提高可读性。

更多的是:大型方法很难测试,因此您可以将#get_feed_facebook分解为小部分(比如构建params、循环等),以提高您的可测试性。

票数 2
EN

Stack Overflow用户

发布于 2016-02-16 14:26:20

在我看来,您想要实现的是更多的功能/验收测试,而不是单元测试。我个人认为,在单元测试中,您应该隔离单元(方法),尝试注入所需的依赖项(模拟),并评估函数的输出(expect和assert)。

在您的示例中,我认为您可以期望使用sdk方法,例如,您有以下方法:

代码语言:javascript
运行
复制
def do_something_with_facebook
  @graph = Koala::Facebook::API.new(oauth_access_token)
end

在这种情况下,我将编写一个测试,检查您的方法是否像下面这样调用Koala::Facebook::API

代码语言:javascript
运行
复制
def test_method_calls_koalla
    grape = mock
    Koala::Facebook
      .expects(:new)
      .returns(grape)

    method = do_something_with_facebook
  end

它可能不是rspec语法,但我希望它能给您一些想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35433203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档