嗨,我正在為 Mailgun api 構建一個 Rails/Rack 中間件,并對連接進行簡單的測驗,這在 Postman 中可以使用完全相同的引數。
它在測驗中顯示錯誤,失敗/錯誤:@app.call(env)
NoMethodError:
undefined method `merge!' for #<Mailgun::Tracking::MailLogger:0x0000557e6bc8bb50>
在運行 RSpec 時,根本沒有嘗試合并方法,所以不知道它來自哪里。api_key 和端點根據 Mailgun Tracking gem(正在使用)指南在 config/initialisers 檔案夾中初始化。
僅使用了其他兩個檔案和下面的代碼。
Mailgun::Tracking 模塊下的MailLogger 類,在app 中注冊為中間件。
require 'logger'
require 'mailgun/tracking'
require 'rack/contrib'
require 'rack'
require 'byebug'
module Mailgun::Tracking
class MailLogger
def initialize(app, options = {
"HTTP_X_MAILGUN_API_KEY" => ENV['MAILGUN_API_KEY'],
"HTTP_X_MAILGUN_ENDPOINT" => ENV['MAILGUN_ENDPOINT']
})
@app = app
@options = options
end
def call(env)
@app.call(env)
end
end
end
RSpec 測驗
require 'logger'
require "json"
require "rails_helper"
Bundler.require(*Rails.groups)
require_relative "../../lib/middleware/mailgun/mailgun_tracking.rb"
RSpec.describe Mailgun::Tracking::MailLogger, type: 'Mailgun Webhook' do
subject(:response) { app.call(env) }
# env to pass in middleware, url endpoint & key
let(:app) { described_class.new(Rails.application) }
let(:env) { described_class.new(app, options = {
"HTTP_X_MAILGUN_API_KEY" => ENV['MAILGUN_API_KEY'],
"HTTP_X_MAILGUN_ENDPOINT" => ENV['MAILGUN_ENDPOINT']
}) }
it "returns a 200 status code" do
expect(response).to eq([200, {}, ["OK"]])
end
end
我只是在尋找一個 200 的連接,OK,因為我已經使用相同的標頭(api_key)和端點(事件)進入 Postman
但拋出這個神秘的失蹤“合并”方法錯誤
以前沒遇到過。
有誰知道為什么?
謝謝
uj5u.com熱心網友回復:
實際的代碼呼叫merge!隱藏在機架實作中,因此從錯誤訊息中過濾掉。您的問題是env您在測驗中定義的不是中間件的實體,Rack::Environment而是中間件的實體。您可以使用例如生成模擬環境實體,Rack::MockRequest.env_for("/", method: :get)
話雖如此,您的單元測驗目前正在測驗整個機架堆疊,包括路由和其他中間件。您可以使用模擬應用程式更好地隔離您的測驗:
let(:app) { described_class.new(->(env) [200, {}, 'success']) }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489080.html
