![](/img/trans.png)
[英]Testing Rack Middleware with RSpec to see if a “service” of the middleware loaded properly
[英]Testing a rack service that relies on another rack service
我正在重構一個中型Web應用程序,其中包含許多使用RSpec編寫的單元測試和驗收測試。 我的主要目標是為微服務清理代碼,該微服務在生產中作為單獨的機架應用程序運行。 當前的測試套件使用流程管理來啟動和停止微服務,這導致測試性能出現問題,並使測試環境如何管理測試夾具數據變得復雜。
有成百上千的測試依賴於微服務的正確行為,這意味着在此重構過程中,我將沒有機會完全模擬或偽造微服務本身。 但是我真的很想刪除所有單獨的流程處理和相關的成本。 現在這是可行的,因為我要為微服務本身添加單元測試,並且我們有一個單獨的冒煙和集成測試基礎結構,該基礎結構已經與作為單獨的基於機架的服務一起處理的微服務一起運行。
我創建了一個SSCE來演示該問題。 它包含在多個小文件中,具有完整的功能並包含測試。 對總長度表示歉意,但我認為這是不可避免的,因為我的問題是如何將這些多個組成部分結合在一起。
微服務/ app.rb
require "grape"
class Microservice < Grape::API
format :json
get '/main' do
{ :message => "Hello World!" }
end
end
微服務/ config.ru
require File.join(File.dirname(__FILE__), "app")
run Microservice
微服務/規格/ spec_helper.rb
require_relative '../app.rb'
require 'rspec'
require 'rack/test'
def app
Microservice
end
RSpec.configure do |config|
config.include Rack::Test::Methods
end
微服務/規格/ microservice_spec.rb
require_relative 'spec_helper'
describe Microservice do
describe "GET /main" do
it "responds with correct JSON" do
get "/main"
expect( last_response ).to be_ok
data = JSON.parse( last_response.body )
expect( data ).to be == { "message" => "Hello World!" }
end
end
end
Web應用程序/ app.rb
require 'sinatra'
require 'json'
require 'httparty'
# This stands in for more complex config we have in reality
$microservice_url = 'http://127.0.0.1:8090/'
get '/main' do
# Calls a microservice . . . (annoyingly the service uses the same route name)
response = HTTParty.get( $microservice_url + "main" )
data = JSON.parse( response.body )
"#{data['message']}\n"
end
web應用/規格/ spec_helper.rb
require_relative '../app.rb'
require 'rspec'
require 'rack/test'
def app
Sinatra::Application
end
RSpec.configure do |config|
config.include Rack::Test::Methods
end
# Is there something I can do here to load the
# microservice without launching it in a new process, and route
# the HTTParty.get in the main app to it?
$microservice_url = 'http://127.0.0.1:8888/'
web應用/規格/ webapp_spec.rb
require_relative 'spec_helper'
describe "Main web app" do
describe "GET /main" do
it "responds with correct text" do
get "/main"
expect( last_response ).to be_ok
expect( last_response.body ).to include "Hello World!"
end
end
end
我可以輕松運行微服務測試:
rspec -f d -c microservice/spec/microservice_spec.rb
但是要運行webapp測試,我首先需要在測試希望找到它的地方啟動微服務:
rackup -p 8888
(不同的過程)
rspec -f d -c webapp/spec/webapp_spec.rb
我認為我可以通過查看諸如如何在另一個Sinatra應用程序中安裝Sinatra應用程序之類的問題來將微服務安裝在應用程序中? 但這似乎適合在生產環境中加入應用程序,我需要將它們分開,僅在幫助程序啟用它的單元測試中加入,到目前為止我完全不知道如何告訴HTTParty(或任何可能的替代方法)做我想在這里進行的連接)。
這是我可能在示例中(在webapp / spec / spec_helper.rb結尾 )對單個調用進行存根的方法-是否可以將其路由到微服務的進程內掛載?
require 'webmock/rspec'
include WebMock::API
stub_request( :any, /:8888\// ).to_return( :body => '{ "message":"Hello World!"}' )
WebMock提供到Rack響應的路由 ,因此可以打包microservice
以便webapp
可以使用它,然后您可以:
require 'microservice'
stub_request( :any, /:8888\// ).to_rack( Microservice )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.