[英]Continuous third party APIs monitoring & testing on Rails
我们希望设置自动化作业(通过Jenkins
)以警告第三方API是否已关闭或者他们是否部署了不兼容的API。
我正在谈论测试真正的HTTP APIs
而不是模拟,但由于我们已经使用rspec
进行模拟编写,我不确定是否应该通过编写两个独立的测试来复制工作。
以前有人有这方面的经验吗? (如果其他工具可以提供帮助,我不限于Ruby/Rspec
)
几个月前,当我遇到这种情况时,我做了以下事情:
我是这样做的,因为我无法猜测/知道实时API将提供哪些内容。
模拟用于测试您自己的代码,无需接触真实的API。 而且你想测试真正的API。
所以我认为你必须在RSpec中编写一组测试,例如对第三方API进行不引人注意的测试。
“不引人注目”我指的是跟踪,例如,您不会发出意外的“DELETE”API请求,或者通过单个测试套件运行使用所有每日请求API限制。
不知道是否存在指定的API测试工具。
至于我,我使用RSpec成功测试了我自己的远程API /服务器。
我要对现有的测试套件做两件事,所以它可以实时使用,第一件使用describe
, it
阻止采用元数据( 这里有一篇很好的博客文章 )。 第二个使用shared_contexts
来阻止。
首先,使用元数据标记您要针对真实API运行的规范。 例如,你想知道这些可以真实运行,例如
describe "Hitting the API with a call", :can_be_real do
# …
end
然后可以使用tag选项从命令行运行这些规范。
第二件事,是用真实的东西取代嘲笑。 这取决于你如何定义的嘲笑,无论是before
还是let
用,而你多少嘲笑。 作为一个愚蠢的例子,见下文:
require 'rspec'
RSpec.configure do |c|
c.treat_symbols_as_metadata_keys_with_true_values = true
end
shared_context "all my mocks and stubs" do
let(:this) { false }
end
describe "a", :real do
include_context "all my mocks and stubs" do
let(:this) { true } if ENV["REAL_API_CALL"] == 'true'
before do
stub_const( "URI", Class.new ) unless ENV["REAL_API_CALL"] == 'true'
end
end
it "should be real when it's real" do
this.should == true
end
it "should escape things when it's real" do
URI.should respond_to :escape
end
end
当文件通过bin/rspec example.rb
运行时,输出为:
a
should be real when it's real (FAILED - 1)
should escape things when it's real (FAILED - 2)
Failures:
1) a should be real when it's real
Failure/Error: this.should == true
expected: true
got: false (using ==)
# ./example.rb:19:in `block (2 levels) in <top (required)>'
2) a should escape things when it's real
Failure/Error: URI.should respond_to :escape
expected URI to respond to :escape
# ./example.rb:22:in `block (2 levels) in <top (required)>'
Finished in 0.00349 seconds
2 examples, 2 failures
当通过env REAL_API_CALL=true bin/rspec example.rb
:
a
should be real when it's real
should escape things when it's real
Finished in 0.00301 seconds
2 examples, 0 failures
所以你看,你可以通过几种方式改变规范的上下文,从而允许你从命令行(因此,Jenkins)获得你想要的控制级别。 您可能希望使用其他元数据标记规范,例如是否可以安全地运行,是否可能需要很长时间等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.