[英]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.