如何对使用带有RSpec的REST API的方法进行单元测试?(How can I unit

编程入门 行业动态 更新时间:2024-10-28 18:32:44
如何对使用带有RSpec的REST API的方法进行单元测试?(How can I unit-test a method that uses a REST API with RSpec?)

我正在开发一个从外部REST API(来自Facebook,Twitter或Instagram等社交网络)获取数据的项目。

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

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

如果它是正确的,我如何使用RSpec进行测试? 有没有我可以阅读的指南或来源? 如果这是错的,那我该怎么检查呢? 如果我将API_VERSION更改为更高版本,我怎么知道逻辑仍然运行良好,所有必填字段仍然存在?

例如:

我有这样的代码:

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

I'm working on a project that gets data from an external REST API (from a social network such as Facebook, Twitter or Instagram).

I'm not sure that what I'm doing is right or wrong so I need some guidance. I don't know how, when people create an app which depends on data from outside (a REST API or crawling data), they do TDD with it.

My question is: I'm trying to do a TDD test on a method that calls an external REST API. Is this right or wrong?

If it's right, how can I test it with RSpec? Is there any guide or source that I can read? If it's wrong, then how can I check it? if I change the API_VERSION to a later version, how can I know that the logic is still working well, and all required fields are still there?

For example:

I have code like this:

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

Then I have a spec like

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

最满意答案

是的,它适用于测试以获得外部服务,但您可以通过几种方式将其对测试套件的影响降至最低。

我会测试这段代码如下:

注册Facebook测试用户 。 编写RSpec功能规范或Cucumber场景,使用测试用户测试在整个堆栈中使用Facebook的整个功能。 使用VCR gem记录Facebook的响应,以便远程调用不会减慢您的测试速度。

为SocialNetwork编写RSpec规范(单元测试)。

在每次不同的Facebook调用的一个或几个单元测试中,让他们使用测试用户点击Facebook,并再次使用VCR来保持他们的速度。 或者,让他们中的一个或几个一直打到Facebook,以便你知道Facebook的API是否会发生变化。 在SocialNetwork的其他规范中,那些只是测试你已经测试过的调用的变种的那些,就会把考拉剔除。

很难准确地解释哪些测试应该击中Facebook以及哪些应该使用存根而不将它们放在我们面前。 如果您需要更多建议,请查看它是如何发布的。

Yes, it's appropriate for tests to hit external services, but you can minimize the impact of that on your test suite in a couple of ways.

I would test this code as follows:

Register a Facebook test user. Write RSpec feature specs or Cucumber scenarios to test the entire feature that uses Facebook through the entire stack, using the test user. Use the VCR gem to record Facebook's responses so that the remote calls don't slow down your tests.

Write RSpec specs (unit tests) for 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.

It's hard to explain exactly which tests should hit Facebook and which should use stubs without having them in front of us. See how it goes and post again if you need more advice.

更多推荐

本文发布于:2023-08-06 03:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1443972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   方法   REST   RSpec   unit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!