From 7b4e583e515444bd346349c135460faf2f905701 Mon Sep 17 00:00:00 2001 From: Tom de Bruijn Date: Thu, 27 Jun 2024 16:56:29 +0200 Subject: [PATCH] Improve Hanami integration testing Improve testing for the Hanami integration by making sure the it prepends the module in every scenario. That means we're not really prepending the module to Hanami::Action in the tests, because that would make testing it multiple times impossible. It's not possible to "unprepend" a module. I've updated the prepended module test to prepend the module to an Action class. First it subclasses the Hanami Action fixture using an anonymous class (using `Class.new`). Then it prepends the module to that anonymous class to see if the behavior works. --- lib/appsignal/integrations/hanami.rb | 2 +- .../lib/appsignal/integrations/hanami_spec.rb | 44 ++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/appsignal/integrations/hanami.rb b/lib/appsignal/integrations/hanami.rb index 6f3da7434..97e99f8e8 100644 --- a/lib/appsignal/integrations/hanami.rb +++ b/lib/appsignal/integrations/hanami.rb @@ -43,4 +43,4 @@ def call(env) end end -Appsignal::Integrations::HanamiPlugin.init +Appsignal::Integrations::HanamiPlugin.init unless Appsignal.testing? diff --git a/spec/lib/appsignal/integrations/hanami_spec.rb b/spec/lib/appsignal/integrations/hanami_spec.rb index 48cb99812..1f64c4403 100644 --- a/spec/lib/appsignal/integrations/hanami_spec.rb +++ b/spec/lib/appsignal/integrations/hanami_spec.rb @@ -5,7 +5,12 @@ require "appsignal/integrations/hanami" before do + Appsignal.config = nil + allow(::Hanami::Action).to receive(:prepend) uninstall_hanami_middleware + ENV["APPSIGNAL_APP_NAME"] = "hanamia-test-app" + ENV["APPSIGNAL_APP_ENV"] = "test" + ENV["APPSIGNAL_PUSH_API_KEY"] = "0000" end def uninstall_hanami_middleware @@ -18,19 +23,21 @@ def uninstall_hanami_middleware describe Appsignal::Integrations::HanamiPlugin do it "starts AppSignal on init" do - expect(Appsignal).to receive(:start) + expect(Appsignal.active?).to be_falsey + Appsignal::Integrations::HanamiPlugin.init + + expect(Appsignal.active?).to be_truthy end it "prepends the integration to Hanami::Action" do - allow(Appsignal).to receive(:active?).and_return(true) Appsignal::Integrations::HanamiPlugin.init - expect(::Hanami::Action.included_modules) - .to include(Appsignal::Integrations::HanamiIntegration) + + expect(::Hanami::Action) + .to have_received(:prepend).with(Appsignal::Integrations::HanamiIntegration) end it "adds middleware to the Hanami app" do - allow(Appsignal).to receive(:active?).and_return(true) Appsignal::Integrations::HanamiPlugin.init expect(::Hanami.app.config.middleware.stack[::Hanami::Router::DEFAULT_PREFIX]) @@ -41,11 +48,16 @@ def uninstall_hanami_middleware end context "when not active" do - before { allow(Appsignal).to receive(:active?).and_return(false) } + before do + ENV.delete("APPSIGNAL_APP_NAME") + ENV.delete("APPSIGNAL_APP_ENV") + ENV.delete("APPSIGNAL_PUSH_API_KEY") + end it "does not prepend the integration to Hanami::Action" do Appsignal::Integrations::HanamiPlugin.init - expect(::Hanami::Action).to_not receive(:prepend) + + expect(::Hanami::Action).to_not have_received(:prepend) .with(Appsignal::Integrations::HanamiIntegration) end @@ -85,14 +97,24 @@ def uninstall_hanami_middleware describe Appsignal::Integrations::HanamiIntegration do let(:transaction) { http_request_transaction } + let(:app) do + Class.new(HanamiApp::Actions::Books::Index) do + def self.name + "HanamiApp::Actions::Books::Index::TestClass" + end + end + end around { |example| keep_transactions { example.run } } - before(:context) { start_agent } before do - allow(Appsignal).to receive(:active?).and_return(true) + ENV["APPSIGNAL_APP_NAME"] = "hanamia-test-app" + ENV["APPSIGNAL_APP_ENV"] = "test" + ENV["APPSIGNAL_PUSH_API_KEY"] = "0000" Appsignal::Integrations::HanamiPlugin.init + allow(app).to receive(:prepend).and_call_original + app.prepend(Appsignal::Integrations::HanamiIntegration) end - def make_request(env, app: HanamiApp::Actions::Books::Index) + def make_request(env) action = app.new action.call(env) end @@ -117,7 +139,7 @@ def make_request(env, app: HanamiApp::Actions::Books::Index) make_request(env) expect(transaction.to_h).to include( - "action" => "HanamiApp::Actions::Books::Index" + "action" => "HanamiApp::Actions::Books::Index::TestClass" ) end end