Skip to content

Commit

Permalink
Improve Hanami integration testing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tombruijn committed Jun 27, 2024
1 parent 5274e6d commit 7b4e583
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/appsignal/integrations/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def call(env)
end
end

Appsignal::Integrations::HanamiPlugin.init
Appsignal::Integrations::HanamiPlugin.init unless Appsignal.testing?
44 changes: 33 additions & 11 deletions spec/lib/appsignal/integrations/hanami_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7b4e583

Please sign in to comment.