我正在从事一个从外部REST (从Facebook、Twitter或Instagram等社交网络)获取数据的项目。
我不确定我所做的是对是错,所以我需要一些指导。我不知道,当人们创建一个依赖于外部数据( REST或爬行数据)的应用程序时,他们如何使用它进行TDD。
,我的问题是:我试图对调用外部REST的方法进行TDD测试。这是对还是错?
例如,:
我有这样的代码:
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那我就有了一个规范
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发布于 2016-02-16 16:29:30
是的,测试访问外部服务是合适的,但您可以通过几种方式将其对测试套件的影响降到最低。
我将对此代码进行如下测试:
SocialNetwork规范(单元测试)。- 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上进行,哪些测试应该使用存根而不让它们出现在我们面前。看看进展如何,如果你需要更多的建议,可以再发一次。
发布于 2016-02-16 16:38:16
TDD用于测试作为一个单元的方法。来自外部的数据可以是嘲弄,因此您可以涵盖每一种场景。有点像
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、循环等),以提高您的可测试性。
发布于 2016-02-16 14:26:20
在我看来,您想要实现的是更多的功能/验收测试,而不是单元测试。我个人认为,在单元测试中,您应该隔离单元(方法),尝试注入所需的依赖项(模拟),并评估函数的输出(expect和assert)。
在您的示例中,我认为您可以期望使用sdk方法,例如,您有以下方法:
def do_something_with_facebook
@graph = Koala::Facebook::API.new(oauth_access_token)
end在这种情况下,我将编写一个测试,检查您的方法是否像下面这样调用Koala::Facebook::API。
def test_method_calls_koalla
grape = mock
Koala::Facebook
.expects(:new)
.returns(grape)
method = do_something_with_facebook
end它可能不是rspec语法,但我希望它能给您一些想法。
https://stackoverflow.com/questions/35433203
复制相似问题